-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_v2.py
68 lines (59 loc) · 2.09 KB
/
main_v2.py
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
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 5 09:50:38 2019
1.读取docx文件,将翻译完的内容和英文内容一起保存在新文件中
2.增加显示进度
@author: XL
"""
import os
from docx import Document
from docx.oxml.ns import qn
from Translator import Translator
def read_file(file_name):
document = Document(file_name)
file_content = []
for con in document.paragraphs:
file_content.append(con.text)
return file_content
# 相比前版本改动此处函数,但兼容上一版本的此函数
def save_file(content, dict_content=None):
document = Document()
document.styles['Normal'].font.name = u'宋体'
document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
document.styles['Body Text'].font.name = u'Time New Roman'
document.styles['Body Text']._element.rPr.rFonts.set(qn('w:english'), u'Time New Roman')
hot = True
if dict_content==None:
hot = False
for p_n in range(len(content)):
if hot:
document.add_paragraph(dict_content[p_n], style='Body Text')
document.add_paragraph(content[p_n])
document.save(content[0][0:5]+'.docx') # 保存文档
def translate(dict_content):
content = {}
translator = Translator()
for index, con in dict_content.items():
if len(con) < 7:
content[index] = con
else:
con1 = translator.translate(con)
print('Translate para %s/%s' %(index, len(dict_content)))
content[index] = con1
return content
def main():
path = r'C:\Users\XL\Documents\GitHub\GoogleTranslator'.replace('\\', '/')
file_list = ['t_3. Manuscript.docx', 't_4. Manuscript.docx']
for f in file_list:
file = os.path.join(path, f)
file_content = read_file(file)
index = range(0, len(file_content))
dict_content = dict(zip(index, file_content))
# pool = Pool(4)
# pool.map(translate, dict_content)
# pool.close()
# pool.join()
content = translate(dict_content)
save_file(content, dict_content)
if __name__ == '__main__':
main()