I have a bunch of excel file that I need to translate in to English so I tried to create a python program to do it since I found limitation with Translate My Sheet. I managed to read and translate each cell using xlrd and googletrans, but I run in to a problem where I can't write back to the original file with xlwt.
The code that I managed to come up with
import xlrd
import xlwt
from xlwt import Workbook
from googletrans import Translator
translator =Translator()
location = (r'test.xlsx')
#Writing to file
wb_w = Workbook()
sheet1 = wb_w.add_sheet('sheet 1')
#Reading file
wb_r = xlrd.open_workbook(location)
sheet = wb_r.sheet_by_index(0)
sheet.cell_value(0,0)
#Going through each cell to translate and rewriting
for column in range(sheet.nrows):
for row in range(sheet.ncols):
print(sheet.cell_value(column, row))
sheet1.write(column, row, translator.translate(sheet.cell_value(column, row), dest='en').text)
wb_w.save(r'test.xlsx')
This is the error I'm getting
Traceback (most recent call last):
File "C:/Users/bobi_/PycharmProjects/Translator/Translator.py", line 22, in <module>
sheet1.write(column, row, translator.translate(sheet.cell_value(column, row), dest='en').text)
File "C:\Users\bobi_\PycharmProjects\Translator\venv\lib\site-packages\googletrans\client.py", line 182, in translate
data = self._translate(text, dest, src, kwargs)
File "C:\Users\bobi_\PycharmProjects\Translator\venv\lib\site-packages\googletrans\client.py", line 78, in _translate
token = self.token_acquirer.do(text)
File "C:\Users\bobi_\PycharmProjects\Translator\venv\lib\site-packages\googletrans\gtoken.py", line 195, in do
tk = self.acquire(text)
File "C:\Users\bobi_\PycharmProjects\Translator\venv\lib\site-packages\googletrans\gtoken.py", line 140, in acquire
for i in text:
TypeError: 'float' object is not iterable
Thanks for the help