Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13951

How to write to multiple excel files using pd.ExcelWriter based on different conditions

$
0
0

I've used pd.ExcelWriter before, and I was successfully able to write to multiple Excel worksheets within a single Excel file. Now, I am trying to write to a single worksheet within multiple excel files. One issue I'm having, however, to determine what file pd.ExcelWriter should write to, the function will need to be placed in a loop. This causes the function to repeatedly write the same thing again and again based on the conditional loop, and may sometimes fail to reach the other logic to write to the other excel files in the directory. I can't think of any other way to determine what file to write to and then proceed with the ExcelWriter function. Here is the relevant code:

# Iterate through the time periodsfor period_name, period in time_periods.items():    target_file = get_target_file(period_name)    if target_file:        file_path_out = os.path.join(output_directory, target_file)        # Initialize pd.ExcelWriter for the current target file        with pd.ExcelWriter(file_path_out, mode='a', engine='openpyxl', if_sheet_exists='overlay') as writer:            for key, fin_data in fin_data_dict.items():                period, approach = key            ###Further logic to determine where and with what data to write in the respective excel file(s)###

Any assistance or tips will be much appreciated. Thanks!


Viewing all articles
Browse latest Browse all 13951

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>