我有一个excel 模板,该模板具有多个包含美国(MTHLY-CA...etc)
的工作表
我还有一个数据框,其中包含列StateID
和premiumtest
这个想法是循环遍历工作表名称,并将其与数据框中的StateID列进行比较。如果状态匹配,则将premiumtest
值写入模板中的特定单元格。
import openpyxl
# getting workbook
wb = openpyxl.load_workbook(r'\server\user\Python\Template.xlsx')
# looping through worksheets
for sheet in wb.worksheets:
sheetnames = sheet
print(sheetnames)
for index, row in df.iterrows():
if any(x in 'MTHLY-'+ row[0] for x in sheetnames): #[0]is the index for column StateID
# Then write premiumtest into cell 1
但是我得到一个错误:
Traceback (most recent call last):
if any(x in 'MTHLY-'+ row[0] for x in sheetnames): #
File "x:\Documents\Python\Treaty Year Report\TreatyYearReport3 - Copy.py", line 68, in <genexpr>
if any(x in 'MTHLY-'+ row[0] for x in sheetnames): #
TypeError: 'in <string>' requires string as left operand, not tuple
print(sheetnames):。
答案 0 :(得分:2)
获取代码
any(x in 'MTHLY-'+ row[0] for x in sheetnames)
运行正常,您可以将工作表名称获取为
sheetnames = [sheet.title for sheet in wb.worksheets]
基本上可以代替您当前的for循环
for sheet in wb.worksheets:
sheetnames = sheet
print(sheetnames)
答案 1 :(得分:2)
您正在将单个工作表对象与字符串进行比较。这就是问题。在下面的代码中,您将遍历每个工作表,然后覆盖sheetnames
变量。
for sheet in wb.worksheets:
sheetnames = sheet
print(sheetnames)
您希望获取的是worksheet.title
的列表,因此,更多类似的内容:
sheetnames = [sheet.title for sheet in wb.worksheets]