Seite 1 von 1

Benutzung von python-docx und googletrans

Verfasst: Mittwoch 14. November 2018, 16:56
von kieck
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

Re: Benutzung von python-docx und googletrans

Verfasst: Mittwoch 14. November 2018, 17:00
von __deets__
Bitte Code-Tags benutzen, das ist so unlesbar. Findest du im "vollstaendigen Editor". Wenn du deinen Beitrag nicht mehr bearbeiten kannst, poste bitte nochmal neu.

Re: Benutzung von python-docx und googletrans

Verfasst: Mittwoch 14. November 2018, 17:03
von __blackjack__
@kieck: Komplette Fehlermeldung inklusive Traceback wäre auch praktisch, damit man nicht raten muss was und wo falsch läuft. :-)

Re: Benutzung von python-docx und googletrans

Verfasst: Mittwoch 14. November 2018, 17:10
von kieck
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'

Re: Benutzung von python-docx und googletrans

Verfasst: Mittwoch 14. November 2018, 17:56
von __deets__
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.

Re: Benutzung von python-docx und googletrans

Verfasst: Donnerstag 15. November 2018, 11:51
von kieck

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)
mit code tags, geht es besser so?

Re: Benutzung von python-docx und googletrans

Verfasst: Donnerstag 15. November 2018, 12:01
von __deets__
Hast du meine Antwort gelesen?

Re: Benutzung von python-docx und googletrans

Verfasst: Donnerstag 15. November 2018, 12:15
von kieck
ja Antwort habe ich gelesen. Die Hinweise haben nichts gebracht.
Gibt es weitere Doku zur aktuellen googletrans?

Re: Benutzung von python-docx und googletrans

Verfasst: Donnerstag 15. November 2018, 12:27
von __deets__
Den Code. Musst halt debuggen & schauen, was passiert. Oder dich an die Programmierer des Pakets wenden.