My current code is able to do as desired and make changes in all sheets based on filename that starts with a certain string. But, i just realized that some of the sheets within the file may have slightly different names in the months going forward. My code-
import pandas as pd
from openpyxl import load_workbook
import os
cols_to_drop = ['PSI ID','PSIvet Region','PSIvet region num']
column_name_update_map = {'Account name': 'Company Name','Billing address':'Address','Billing city':'City'}
for file in os.listdir("C:/Users/hhh/Desktop/gu/python/PartMatching"):
if file.startswith("PSI"):
dfs = pd.read_excel(file, sheet_name=None)
output = dict()
for ws, df in dfs.items():
if ws in ["Added"]:
continue
if ws in ["New Members 03.22", "PVCC"]: #sheets to avoid
temp = df
temp['Status'] = "Active" if ws == "All Members" else "Cancelled"
#drop unneeded columns
temp = df.drop(cols_to_drop, errors="ignore", axis=1)
#rename columns
temp = temp.rename(columns=column_name_update_map)
#drop empty columns
temp = temp.dropna(how="all", axis=1)
temp['Partner'] = "PSI"
output[ws] = temp
writer = pd.ExcelWriter(f'{file.replace(".xlsx","")} (updated headers).xlsx')
for ws, df in output.items():
df.to_excel(writer, index=None, sheet_name=ws)
writer.save()
writer.close()
My goal is to make my current code avoid the sheet whose name starts with "New Members". But as you can see in my code I have to specifically mention New Members 03.22. This sheet next month will be named New Members 04.22 and so wont be compatible with my code to run on a scheduled task. I tried if ws.startswith in ["New Members 03.22", "PVCC"]: but nothing happened.
CodePudding user response:
startswith
can only be used with one string at a time, so you need to break up the test.
if any(ws.startswith(x) for x in ["New Members", "PVCC"]):