原文地址:
用 Python 写的文档批量翻译工具,效果竟然超越付费软件?_平静愉悦的博客-CSDN博客
接入与错误码文档见百度开放平台:
from docx import Document
from docx.oxml.ns import qn
import requests
import random
import json
from hashlib import md5
import time
import glob
import os
def make_md5(s, encoding='utf-8'):
return md5(s.encode(encoding)).hexdigest()
appid = '替换成自己的xxx'
appkey = '替换成自己的xxx'
from_lang = 'en'
to_lang = 'zh'
endpoint = 'http://api.fanyi.baidu.com'
path = '/api/trans/vip/translate'
url = endpoint + path
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
filepath = '../source'
for file in glob.glob(filepath + r'/*.docx'):
print(file)
wordfile = Document(file)
wordfile_new = Document()
sections = wordfile.sections
sec = sections[0]
sections_new = wordfile_new.sections
sec_new = sections_new[0]
sec_new.left_margin = sec.left_margin
sec_new.right_margin = sec.right_margin
sec_new.top_margin = sec.top_margin
sec_new.bottom_margin = sec.bottom_margin
sec_new.header_distance = sec.header_distance
sec_new.orientation = sec.orientation
sec_new.page_height = sec.page_height
sec_new.page_width = sec.page_width
for paragraph in wordfile.paragraphs:
if not paragraph.text:
wordfile_new.add_paragraph('\n')
print("not paragraph.text")
continue
print("continue")
size_lst = []
bold_lst = []
italic_lst = []
color_lst = []
highlight_color_lst = []
underline_lst = []
strike_lst = []
double_strike_lst = []
subscript_lst = []
superscript_lst = []
for run in paragraph.runs:
size_lst.append(run.font.size)
bold_lst.append(run.font.bold)
italic_lst.append(run.font.italic)
color_lst.append(run.font.color.rgb)
highlight_color_lst.append(run.font.highlight_color)
underline_lst.append(run.font.underline)
strike_lst.append(run.font.strike)
double_strike_lst.append(run.font.double_strike)
subscript_lst.append(run.font.subscript)
superscript_lst.append(run.font.superscript)
p = wordfile_new.add_paragraph()
p.paragraph_format.alignment = paragraph.paragraph_format.alignment
p.paragraph_format.left_indent = paragraph.paragraph_format.left_indent
p.paragraph_format.right_indent = paragraph.paragraph_format.right_indent
p.paragraph_format.first_line_indent = paragraph.paragraph_format.first_line_indent
p.paragraph_format.line_spacing = paragraph.paragraph_format.line_spacing
p.paragraph_format.space_before = paragraph.paragraph_format.space_before
p.paragraph_format.space_after = paragraph.paragraph_format.space_after
query = paragraph.text
if query == '\n':
continue
salt = random.randint(32768, 65536)
sign = make_md5(appid + query + str(salt) + appkey)
payload = {'appid': appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
print(payload)
r = requests.post(url, params=payload, headers=headers)
result = r.json()
print(result)
for res in result['trans_result']:
p.add_run(res['dst'])
for run in p.runs:
run.font.name = '微软雅黑'
r = run._element.rPr.rFonts
r.set(qn('w:eastAsia'), '微软雅黑')
run.font.size = max(size_lst, key=size_lst.count)
run.font.bold = max(bold_lst, key=bold_lst.count)
run.font.italic = max(italic_lst, key=italic_lst.count)
run.font.color.rgb = max(color_lst, key=color_lst.count)
run.font.highlight_color = max(highlight_color_lst, key=highlight_color_lst.count)
run.font.underline = max(underline_lst, key=underline_lst.count)
run.font.strike = max(strike_lst, key=strike_lst.count)
run.font.subscript = max(subscript_lst, key=subscript_lst.count)
run.font.superscript = max(superscript_lst, key=superscript_lst.count)
wordfile_new.save('translated.docx')