I have a work process that I am automating. I believe this would be a very familiar scenario to many others. The task is somewhat like this:
- Download zip files from mail (csv files)
- extract values from the csv files
- paste these values into the corresponding sheet in a master excel file (whilst retaining the formatting) - using pandas and python to do the sheet mapping and extracting/pasting of data via pd DataFrames
- save the master file after repeating step 3-4 for all csv files. And fanout as email.
STEP 3 is the problem in particular. At this step I have already fully compiled the data I want and injected it into the respective sheet (as df). I am only left to write this df into the master excel sheet. However, I am still unable to find a method that does this without overriding my excel formatting. It would be really helpful to have a paste as values solution rather than to spend time and lengthening the code by doing an additional formatting step after pasting. Hope to get some help from you experts out there!
Currently exploring with pandas, numpy, openpyxl to solve the solution.
# INJECT ORDERED DATA FROM TEMP DF INTO TRANSACTION METRICS DF
transaction_metrics_df[INPUT_DATETIME] = temp_df["data"]
with pd.ExcelWriter(TRANSACTION_METRICS, engine="openpyxl") as writer:
transaction_metrics_df.to_excel(writer, sheet_name=update_sheet,
index=False)
I understand as of now my code will override the whole master sheet with just one sheet, not because I don't know how to save multiple sheets. Its just that at the moment I am getting one sheet right first. Anyways, getting to the problem with the output, the final xlsx file is saved with correct data, however, all excel formatting that used to exist is now lost e.g. numbers to 3dp, coloured fills, borders... aesthetic problem basically