5

就差一个Python写Excel两列的操作了,吃货不会了。

 3 years ago
source link: https://www.52pojie.cn/thread-1451727-1-1.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
查看: 619|回复: 11

[求助] 就差一个Python写Excel两列的操作了,吃货不会了。

[复制链接]

楼主

发表于 2021-6-2 08:24

[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文件的操作
就是原义列
翻译结果列!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK