-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword.py
64 lines (50 loc) · 1.98 KB
/
word.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
from googletrans import Translator
import os
from util import get_language_code
from docx.api import Document as DocumentRead
from docx import Document as DocumentWrite
class WordTranslator:
def __init__(self) -> None:
self.translator = Translator()
self.translate()
def get_word_file(self) -> str:
while True:
file_path = input('Enter the file path: ')
if os.path.exists(file_path):
if file_path.endswith('.docx'):
return file_path
else:
print('Invalid file extension. Try again.')
else:
print('Invalid file path. Try again.')
def get_language_code(self) -> str:
while True:
lang = input('Enter the language: ')
lang_code = get_language_code(lang)
if lang_code:
return lang_code
else:
print('Invalid language. Try again.')
def translate(self) -> None:
file_path = self.get_word_file()
lang_code = self.get_language_code()
# read the file
doc = DocumentRead(file_path)
new_path = file_path.rstrip('.docx') + f'-{lang_code}.docx'
new_doc = DocumentWrite()
# translate the file
for p in doc.paragraphs:
translated = self.translator.translate(p.text, dest=lang_code)
translated_text = translated.text
new_p = new_doc.add_paragraph(translated_text)
new_p.style = p.style
for t in doc.tables:
new_table = new_doc.add_table(rows=len(t.rows), cols=len(t.columns))
for i, row in enumerate(t.rows):
for j, cell in enumerate(row.cells):
new_table.cell(i, j).text = self.translator.translate(cell.text, dest=lang_code).text
new_table.style = t.style
new_doc.save(new_path)
print('Translation complete.')
if __name__ == "__main__":
WordTranslator()