Benutzung von python-docx und googletrans
Verfasst: Mittwoch 14. November 2018, 16:56
Hallo,
ein kleines Codefragment aus der Community heruntergeladen, aber es läuft so nicht bei mir. Ist es alter Programmierstil oder sind veraltete Libraries verwendet worden. Und zwar:
python-docx
googletrans
Habe diese mehrfach installiert, auch mit: pip install --upgrade docx behandelt. Auch die googletrans. Aus den Fehlermeldungen heraus kann ich mir nicht mehr helfen. Ein attribute 'group' wird nicht gefunden.
Muss ich neuere Libraries installieren oder weitere hinzunehmen? Wie kann ich überprüfen, dass ich den letztgültigen Stand der Versionen benutze?
Hier das Codefragement, ich möchte eine Word-Docx Datei auslesen:
from docx import Document
from googletrans import Translator
def translate_doc(filename, destination='zh-CN',encoding='ISO-8859-1'):
""" 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': e.g. en
:param mix=True: if True, will have original language and target language into the same doc. paragraphs by paragraphs.
"""
print('translate_doc of '+filename,'to '+destination)
def tx(t): return Translator().translate(t, dest=destination).text
doc = Document(filename)
txd = []
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__':
# define the input file for translation, docx-file used in this case
filename = 'C:/DataMining/GW_SOP/SOP_deutsch/05.05.001.docx'
translate_doc(filename)
vielen Dank für Hilfestellung!
Oliver Kieckhöfel
ein kleines Codefragment aus der Community heruntergeladen, aber es läuft so nicht bei mir. Ist es alter Programmierstil oder sind veraltete Libraries verwendet worden. Und zwar:
python-docx
googletrans
Habe diese mehrfach installiert, auch mit: pip install --upgrade docx behandelt. Auch die googletrans. Aus den Fehlermeldungen heraus kann ich mir nicht mehr helfen. Ein attribute 'group' wird nicht gefunden.
Muss ich neuere Libraries installieren oder weitere hinzunehmen? Wie kann ich überprüfen, dass ich den letztgültigen Stand der Versionen benutze?
Hier das Codefragement, ich möchte eine Word-Docx Datei auslesen:
from docx import Document
from googletrans import Translator
def translate_doc(filename, destination='zh-CN',encoding='ISO-8859-1'):
""" 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': e.g. en
:param mix=True: if True, will have original language and target language into the same doc. paragraphs by paragraphs.
"""
print('translate_doc of '+filename,'to '+destination)
def tx(t): return Translator().translate(t, dest=destination).text
doc = Document(filename)
txd = []
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__':
# define the input file for translation, docx-file used in this case
filename = 'C:/DataMining/GW_SOP/SOP_deutsch/05.05.001.docx'
translate_doc(filename)
vielen Dank für Hilfestellung!
Oliver Kieckhöfel