Create translate_word_doc.py (#46)

* Create translate_word_doc.py

a useful example.

* Rename sample/translate_word_doc.py to example/translate_word_doc.py
This commit is contained in:
paulg 2017-10-25 14:51:53 +11:00 committed by ssut
parent 830af19de0
commit f9b1ffd168
1 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,30 @@
from docx import Document
from googletrans import Translator
def translate_doc(filename, destination='zh-CN', mix=True):
"""
translate a word document type of file and save the result as document and keep the exactly same file format.
:param filename: word doc file
:param destination='zh-CN':
:param mix=True: if True, will have original language and target language into the same doc. paragraphs by paragraphs.
"""
def tx(t): return Translator().translate(t, dest=destination).text
doc = Document(filename)
for p in doc.paragraphs:
txd = tx(p.text)
p.text = p.text + ('\n' + txd if mix else '')
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
txd = tx(cell.text)
p.text = cell.text + ('\n' + txd if mix else '')
f = filename.replace('.doc', destination.lower() + '.doc')
doc.save(f)
if __name__ == '__main__':
filename = 'p1.docx'
translate_doc(filename)