The below code currently translate each words from the Excel sheet for the words location in Column A, But the results it currently gives me in the editor but I want the translated output/result in the same excel sheet in Column B. The below code gives me an error. please help me with the code for the results to be written in excel in column B.
import xlrd
import goslate
loc = r"C:\path\fruits.xlsx"
gs = goslate.Goslate()
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
for i in range(sheet.nrows):
print(gs.translate(sheet.cell_value(i, 0), 'de'))
print(sheet.cell_value(i, 1)
I am receiving the below error
return self._cell_values[rowx][colx]
IndexError: list index out of range
Please someone help me to write my output/result in the same excel in Column B
The error is caused because you don't have a
B
is empty and package does not read the column from sheet.To write the translated result to the sheet, you can do something like this:
I don't think
xlrd
can write to sheet. You will need to usexlwt
package. You will need to install itpip install xlwt
As for making changes in the same file, I my opinion you should not do that. The file is already opend by another process in read mode. Changing it can result in unexpected behavior. But if you intent to do that, backup your file, and set the loc for both when reading and when saving file.