Saravananslb / py-googletranslation

pygoogletranslation: Free and Unlimited Google translate API for Python. Translates totally free of charge.
https://saravananslb.github.io/py-googletranslation/
MIT License
156 stars 40 forks source link

"'Nonetype' object is not subscriptable" error on bulk translate #9

Open kyle-sama opened 3 years ago

kyle-sama commented 3 years ago

Tried to use bulk translate on a pdf file:

from pygoogletranslation import Translator

translator = Translator()

translation = translator.translate('日本語')
print(translation)
>>Translated(src=ja, dest=en, text=Japanese, pronunciation=, original_text=日本語 ,extra_data="{}...")
bulk = translator.bulktranslate('annotation_ID07.pdf', src='ja', dest='en')

got this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-6655f63a5be2> in <module>
----> 1 bulk = translator.bulktranslate('annotation_ID07.pdf', src='ja', dest='en')

~\AppData\Roaming\Python\Python37\site-packages\pygoogletranslation\translate.py in bulktranslate(self, file, src, dest)
    257         text = text.replace('"', '')
    258         text = text.replace("'", "")
--> 259         data = self._translate(text, src=src, dest=dest)
    260         return self.extract_translation(data, text)
    261 

~\AppData\Roaming\Python\Python37\site-packages\pygoogletranslation\translate.py in _translate(self, text, src, dest)
    185                     raise Exception('Unexpected status code {} from {}'.format(response.status_code, self.transurl))
    186                     return False
--> 187             translated_list.append(utils.format_translation(trans_list))
    188         return translated_list
    189 

~\AppData\Roaming\Python\Python37\site-packages\pygoogletranslation\utils.py in format_translation(translated)
     90     pron = ''
     91     for _translated in translated:
---> 92         text += _translated[0][2][1][0][0][5][0][0]
     93         try:
     94             pron += unidecode.unidecode(_translated[0][2][1][0][0][1])

TypeError: 'NoneType' object is not subscriptable
Saravananslb commented 3 years ago

Hi kyle-sama,

Please share the pdf to saravananslb2015@gmail.com

I will help you on this issue bcz I have tested with some pdf files that is working file.

So I need a PFD file that you are trying to translate.

Thanks, Saravananslb

kyle-sama commented 3 years ago

I'm sorry, it's sensitive data that I cannot share, but I'll see if I can try on something else

kyle-sama commented 3 years ago

yeah other files don't throw the errors, but give various results...

Saravananslb commented 3 years ago

Try this version pip install pygoogletranslation==2.0.4.

I hope this will fix ur error.

Please let me know ur feedback.

bjquinniii commented 3 years ago

Random thought about the pdf file that wasn't working... what kind of pdf is it? What I mean by that is that if it's really a scanned pdf, then you're unlikely to have much luck with it without some fairly sophisticated ocr.