我的数据框如下所示:
我有包含多个页面的Example.xlsx电子表格:
目标是遍历daraframe StateID
列并与excel工作表名称匹配,如果匹配,则写入单元格B14
值premiumtest
wb = openpyxl.load_workbook(r'\\username\Python\Treaty Year Report\Example.xlsx')
ws = [sheet.title for sheet in wb.worksheets]
print(ws)
for index, row in df1.iterrows():
if any(x in 'MTHLY-'+ row['StateID'] for x in ws):
ws['B14'] = row[2]
但是我得到一个错误:
File "x:\Documents\Treaty Year Report\TreatyYearReport3", line 76, in <module>
ws['B14'] = row[2]
TypeError: list indices must be integers or slices, not str
那为什么我不能在工作表中传递单元格?如果传递索引,如何指定特定的单元格?