From f9b1ffd1688d5aaade770aac2022c5fa8913c016 Mon Sep 17 00:00:00 2001 From: paulg Date: Wed, 25 Oct 2017 14:51:53 +1100 Subject: [PATCH] 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 --- example/translate_word_doc.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 example/translate_word_doc.py diff --git a/example/translate_word_doc.py b/example/translate_word_doc.py new file mode 100644 index 0000000..64b893b --- /dev/null +++ b/example/translate_word_doc.py @@ -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)