[Python] 纯文本查看 复制代码 # -*- coding:utf-8 -*-
import sys
from xlrd import open_workbook # xlrd用于读取xld
import xlwt # 用于写入xls
workbook = open_workbook(r'C:\88.xls') # 打开xls文件
sheet_name= workbook.sheet_names() # 打印所有sheet名称,是个列表
sheet = workbook.sheet_by_index(0) # 根据sheet索引读取sheet中的所有内容
sheet1= workbook.sheet_by_name('Sheet1') # 根据sheet名称读取sheet中的所有内容
print(sheet.name, sheet.nrows, sheet.ncols) # sheet的名称、行数、列数
#content = sheet.col_values(1) # 第1列内容
#print(content)
for i in range(sheet.nrows):
def tranlate(source, direction):
import requests
import json
url = "http://api.interpreter.caiyunai.com/v1/translator"
#WARNING, this token is a test token for new developers, and it should be replaced by your token
token = "法王的令牌"
payload = {
"source" : source,
"trans_type" : direction,
"request_id" : "demo",
"detect": True,
}
headers = {
'content-type': "application/json",
'x-authorization': "token " + token,
}
response = requests.request("POST", url, data=json.dumps(payload), headers=headers)
return json.loads(response.text)['target']
#print (sheet1.cell_value(i, 0)) #循环打印第1列,第2列内容: sheet1.cell_value(i, 1)
AAA= sheet1.cell_value(i, 0)
target = tranlate(AAA, "auto2zh")
print(target)
下面还差一个写入到excel文件的操作
就是原义列
和
翻译结果列!
|