[英]results to be written in excel column B
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.下面的代码当前将 Excel 工作表中的每个单词翻译为 A 列中的单词位置,但它目前在编辑器中给我的结果,但我希望在 B 列中的同一个 Excel 工作表中翻译输出/结果。下面的代码给出我一个错误。 please help me with the code for the results to be written in excel in column B.
请帮助我在 B 列中用 excel 编写结果的代码。
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请有人帮我在 B 列的同一个 excel 中写出我的输出/结果
The error is caused because you don't have a B
is empty and package does not read the column from sheet.错误是因为您没有
B
为空并且包未从工作表中读取列。
To write the translated result to the sheet, you can do something like this:要将翻译结果写入工作表,您可以执行以下操作:
I don't think xlrd
can write to sheet.我不认为
xlrd
可以写入工作表。 You will need to use xlwt
package.您将需要使用
xlwt
包。 You will need to install it pip install xlwt
您将需要安装它
pip install xlwt
import xlrd
import xlwt # this package is going to write to sheet
import goslate
loc = "dummy.xlsx"
translated = "dummy2.xlsx" # location to where store the modified sheet
gs = goslate.Goslate()
# crate a workbook using xlwt package in order to write to it.
wbt = xlwt.Workbook() # there is a typo here. this should be wbt
ws = wbt.add_sheet('A Test Sheet') # change this to your sheet name
rwb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
for i in range(sheet.nrows):
ws.write(i, 0, sheet.cell_value(i, 0)) # this will write the A column value
ws.write(i, 1, gs.translate(sheet.cell_value(i, 0), 'de')) # this will write the B column value
wbt.save(translated) # this will save the sheet.
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.
但是,如果您打算这样做,请备份您的文件,并在读取和保存文件时设置 loc。
You're getting the error because xlrd addresses columns are zero based and per documentation the xlrd ignores cells with no data.您收到错误是因为 xlrd 地址列是从零开始的,并且根据文档,xlrd 会忽略没有数据的单元格。
so you could access column A by doing所以你可以通过做访问列A
sheet.cell_value(i, 0)
and write to column B by doing并通过执行写入列 B
sheet._cell_types[i][1] = xlrd.XL_CELL_TEXT
sheet._cell_values[i][1] = source
however xlrd
is only for reading, so you'd have to use xlwt
to save any changes.但是
xlrd
仅用于阅读,因此您必须使用xlwt
来保存任何更改。
Saving changes brings up another issue, you're source file is ".xlsx" extension, while xlrd
does read this format, xlwt
only writes to the older ".xls" format.保存更改会带来另一个问题,您的源文件是“.xlsx”扩展名,而
xlrd
确实读取这种格式, xlwt
只写入旧的“.xls”格式。
To read and write to ".xlsx" format with one library you can use openpyxl
, using this library your code would look like this:要使用一个库读取和写入“.xlsx”格式,您可以使用
openpyxl
,使用此库您的代码将如下所示:
import openpyxl
import goslate
loc = r"C:\path\fruits.xlsx"
gs = goslate.Goslate()
wb = openpyxl.load_workbook(loc)
sheet = wb.active
for i in range(2, sheet.max_row + 1):
original = sheet.cell(row=i, column=1).value
translated = gs.translate(original, 'de')
sheet.cell(row=i, column=2).value = translated
wb.save(loc)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.