简体   繁体   English

结果要写在excel列B中

[英]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.

相关问题 Openpyxl - 从 A 列复制内容,编辑,将结果存储在 B 列中 - Openpyxl - Copy contents from column A, edit, store results in column B 熊猫:添加像= A1 / SUMIF(B:B,B1,A:A)这样的excel SUMIF列 - Pandas: Adding an excel SUMIF column like =A1/SUMIF(B:B,B1,A:A) 如何写入现有excel文件的B列 - How can I write to B column of an existing excel file pandas group_by dataframe 写入 excel 时只输出聚合列; 如何在 excel 上获得整个 output? - pandas group_by dataframe outputs only the aggregation column when written to excel; how to get the entire output on excel? 遍历Excel的A列,使用Python win32写入B列 - Iterate thru Excel column A , write to column B using Python win32 python / excel:如何为列A中的所有相同值添加列B的值 - python/excel: How do I add values of column B for all the same values in column A 如何将结果从一个变量传输到excel中的列? - How can i transfer the results from one variable to a column in excel? 如何计算 excel 中条件单元格的总和,用结果填充另一列 - How to calculate the sum of conditional cells in excel, populate another column with results 在使用 pandas 的 python 中,我希望我的程序在 csv 文件中的 A 列和 B 列中搜索字符串并将结果写入 Z0D650F837D4F243B11 列 - In python using pandas, I want my program to search strings in column A and column B in csv file and write results in column C 如何使用python和openpyxl在excel中搜索特定的列名称(不是A,B等),例如Name,Marks? - How to search specific column name ( not A ,B etc) such as Name ,Marks in excel using python and openpyxl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM