[英]How to add data to specific row/column in an excel sheet using python based on a given list?
[英]How to iterate over a Row or Column of an Excel sheet using Python and look for specific value in cells?
下面是我从某人那里复制的代码。 它读取并更新现有的 Excel 文件。 我想要的是添加另一个迭代每行或每列并查找特定值的功能。 然后我想用新值更新该特定单元格。
import xlwt
import xlrd
from xlutils.copy import copy
rb = xlrd.open_workbook("imtiaz.xls")
wb = copy(rb)
w_sheet = wb.get_sheet(0)
w_sheet.write(0,1,45)
w_sheet.write(1,1,46)
wb.save("imtiaz.xls")
您也可以使用以下逻辑。 在最后一个data(List of List)
包含所有值。 每个内部列表代表行。
import xlrd
book = xlrd.open_workbook('test.xlsx')
sheet = book.sheet_by_index(0)
data = []
for row in range(0, sheet.nrows):
l = []
for column in range(0, sheet.ncols):
val = sheet.cell(row, column).value
l.append(val)
data.append(l)
print(data)
要遍历 Excel 工作表,您可以使用 xlrd 模块中的sheet.nrows()
函数。
import xlrd
workbook = xlrd.open_workbook("my_path") # Opens your file
sheet = workbook.sheet_by_index(0) # Gets the first sheet
for row in range(sheet.nrows): # Iterates over your sheet
row_value = sheet.row_values(row)
if row_value[1] == "my_value":
print row_value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.