У меня есть Excel шаблон , в котором есть несколько листов с США. (MTHLY-CA...etc)
![enter image description here](https://i.stack.imgur.com/Ssx9F.png)
У меня также есть dataframe , в котором есть столбцы StateID
и premiumtest
![enter image description here](https://i.stack.imgur.com/Un0l8.png)
Идея состоит в том, чтобы просмотреть имена рабочих таблиц и сравнить их со столбцом 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):.
![sheetnames](https://i.stack.imgur.com/ZGpwe.png)