下面是我从某人照搬过来的代码。它读取并更新现有的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")
要遍历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