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
Benutzung von python-docx und googletrans
- __blackjack__
- User
- Beiträge: 13071
- Registriert: Samstag 2. Juni 2018, 10:21
- Wohnort: 127.0.0.1
- Kontaktdaten:
@kieck: Komplette Fehlermeldung inklusive Traceback wäre auch praktisch, damit man nicht raten muss was und wo falsch läuft.
„All religions are the same: religion is basically guilt, with different holidays.” — Cathy Ladman
die komplette Fehlermeldung:
translate_doc of C:/DataMining/GW_SOP/SOP_deutsch/05.05.001.docx to zh-CN
Traceback (most recent call last):
File "C:/DataMining/GW_SOP/google_translate02.py", line 29, in <module>
translate_doc(filename)
File "C:/DataMining/GW_SOP/google_translate02.py", line 16, in translate_doc
txd = tx(p.text)
File "C:/DataMining/GW_SOP/google_translate02.py", line 12, in tx
def tx(t): return Translator().translate(t, dest=destination).text
File "C:\Users\oki\AppData\Local\Programs\Python\Python37\lib\site-packages\googletrans\client.py", line 172, in translate
data = self._translate(text, dest, src)
File "C:\Users\oki\AppData\Local\Programs\Python\Python37\lib\site-packages\googletrans\client.py", line 75, in _translate
token = self.token_acquirer.do(text)
File "C:\Users\oki\AppData\Local\Programs\Python\Python37\lib\site-packages\googletrans\gtoken.py", line 180, in do
self._update()
File "C:\Users\oki\AppData\Local\Programs\Python\Python37\lib\site-packages\googletrans\gtoken.py", line 59, in _update
code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'
translate_doc of C:/DataMining/GW_SOP/SOP_deutsch/05.05.001.docx to zh-CN
Traceback (most recent call last):
File "C:/DataMining/GW_SOP/google_translate02.py", line 29, in <module>
translate_doc(filename)
File "C:/DataMining/GW_SOP/google_translate02.py", line 16, in translate_doc
txd = tx(p.text)
File "C:/DataMining/GW_SOP/google_translate02.py", line 12, in tx
def tx(t): return Translator().translate(t, dest=destination).text
File "C:\Users\oki\AppData\Local\Programs\Python\Python37\lib\site-packages\googletrans\client.py", line 172, in translate
data = self._translate(text, dest, src)
File "C:\Users\oki\AppData\Local\Programs\Python\Python37\lib\site-packages\googletrans\client.py", line 75, in _translate
token = self.token_acquirer.do(text)
File "C:\Users\oki\AppData\Local\Programs\Python\Python37\lib\site-packages\googletrans\gtoken.py", line 180, in do
self._update()
File "C:\Users\oki\AppData\Local\Programs\Python\Python37\lib\site-packages\googletrans\gtoken.py", line 59, in _update
code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
AttributeError: 'NoneType' object has no attribute 'group'
Dazu findet man diesen Pullrequest auf der github Seite: https://github.com/ssut/py-googletrans/pull/78
Du kannst probieren, dir den herzunehmen, wie hier beschrieben: https://github.com/ssut/py-googletrans/ ... -434649838
Wobei die Kommentare suggerieren, dass da unter Umstaenden eine Aenderung der API von Google hinter steht, die nicht reparabel ist.
Du kannst probieren, dir den herzunehmen, wie hier beschrieben: https://github.com/ssut/py-googletrans/ ... -434649838
Wobei die Kommentare suggerieren, dass da unter Umstaenden eine Aenderung der API von Google hinter steht, die nicht reparabel ist.
Code: Alles auswählen
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)