-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaidu_Excel_TransAPI.py
More file actions
121 lines (90 loc) · 3.46 KB
/
Baidu_Excel_TransAPI.py
File metadata and controls
121 lines (90 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
import requests
import random
import json
from hashlib import md5
import openpyxl
import time
# name = input('请输入Excel的文件名:')
qianzhui = 'demo'
name = qianzhui+'.xlsx'
# 一次提交可翻译的行数
limit = 4
# User: YzH
appid = '20210325000742262'
appkey = 'nBZkYdALLeoNfFhFxcfT'
# For list of language codes, please refer to `https://api.fanyi.baidu.com/doc/21`
from_lang = 'en'
to_lang = 'zh'
# -------------------------------参数设置完成-------------------------------
endpoint = 'http://api.fanyi.baidu.com'
path = '/api/trans/vip/translate'
url = endpoint + path
# Generate salt and sign
def make_md5(s, encoding='utf-8'):
return md5(s.encode(encoding)).hexdigest()
wb = openpyxl.load_workbook(name)
ws = wb.active
if ws['C1'].value != '标题翻译':
print('格式错误,请检查')
exit()
# 确定提交到循环的次数number
yushu = (ws.max_row-1) % limit
if yushu == 0:
number = int((ws.max_row-1)/limit)
else:
number = (ws.max_row-1)//limit
f = 0 # 新的就是0,断点续跑为读取次数减一
# 开始循环提交翻译
for step in range(f, number):
query = ''
for i in range(2+step*limit, 2+(step+1)*limit): # 第一次,2+0*7,第2行开始
if ws['C'+str(i)].value != None:
query = query+ws['C'+str(i)].value+'\n'
query = query+ws['D'+str(i)].value+'\n'
else:
query = query + '*\n' + '*\n'
print('第{}次读取完成,开始提交数据...'.format(step+1))
print(query)
salt = random.randint(32768, 65536)
sign = make_md5(appid + query + str(salt) + appkey)
# Build request
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
payload = {'appid': appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
# Send request
r = requests.post(url, params=payload, headers=headers)
result = r.json()
print(json.dumps(result, indent=4, ensure_ascii=False)) # 对result可视化显示
# Save in sheet
index = 0
for i in range(2+step*limit, 2+(step+1)*limit): # 第一次,2+0*7,第2行开始
ws['C'+str(i)].value = result['trans_result'][index]['dst']
ws['D'+str(i)].value = result['trans_result'][index+1]['dst']
index = index+2
wb.save(qianzhui+'_翻译版.xlsx')
time.sleep(2.6)
# 如果有余数再提交最后一遍
if yushu != 0:
query = ''
for i in range(2 + number * limit, 2 + number * limit + yushu):
query = query + ws['C' + str(i)].value + '\n'
query = query + ws['D' + str(i)].value + '\n'
print('第{}次读取完成,开始提交数据...'.format(number+1))
print(query)
salt = random.randint(32768, 65536)
sign = make_md5(appid + query + str(salt) + appkey)
# Build request
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
payload = {'appid': appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
# Send request
r = requests.post(url, params=payload, headers=headers)
result = r.json()
print(json.dumps(result, indent=4, ensure_ascii=False)) # 对result可视化显示
# Save in sheet
index = 0
for i in range(2 + number * limit, 2 + number * limit + yushu):
ws['C' + str(i)].value = result['trans_result'][index]['dst']
ws['D' + str(i)].value = result['trans_result'][index + 1]['dst']
index = index + 2
wb.save(qianzhui+'_翻译版.xlsx')
print('翻译完成!!!')