Saving the panda’s DataFrame data in an excel file keeping the format of the sheet.
In this article I expose my workaround to save data in a excel file where I have already set the format of the sheet.
Often I need read data from an excel file, put them in a panda’s DataFrame, treat them using data and/or rules from another file and save them in a new file; and Iwish that this file has an official (or company) format.
With the help of stackowerflow I learned that i can use StyleFrame to obtain this result and I tryed it; but when I use the StyleFrame save method to save the data on the Excel sheet I got this error:
export_df.index = [row_index.value for row_index in export_df.index] AttributeError: ‘int’ object has no attribute ‘value’
The solutions that I found are or to fill the file with dummy values or write the data cell by cell. But if Imust write the data cell by cell I don’t need StyleFrame, so I decided to share here my workarond to this issue.
This is my workaround:
- take an empty excel file as template
- save a copy of it with the name of the oputput file that You must generate
- open the file using openpyxl
- use this piece of code to save Your data in Your preformatted excel sheet
I hope that this simple workaround can help also somebody else.