pythonpandasdataframestyleframe

Styleframe module - read_excel_as_template doesn't work, outputs a file with no style


I'm creating a program that inserts pandas DataFrames, styled using template files with the read_excel_file_as_template function from the styleframe module, in excel files which are then sent as attachments in e-mails.

The idea here is to have a folder (named template_files) containing template files that only have formatting and styling done, with no data. We'll then use read_excel_file_as_template to insert the dataframe's data into a new excel file, using the template file's styling.

The issue is that the template file's styling isn't used apparently, and the output file has no styling.

Here is a very simplified version of my code, without error handling and with only indispensable parameters.

from pandas import DataFrame,ExcelWriter
from pandas._libs.tslibs.timestamps import Timestamp
from styleframe import StyleFrame


def Insert_Dataframe_Data_Into_Template(dataframe):
        path_template_file = f"template_files\example_template.xlsx"
        
        styleframe = StyleFrame.read_excel_as_template(path_template_file, dataframe, use_df_boundaries=True)
            
        with ExcelWriter("output_file.xlsx", engine="openpyxl") as writer:
            styleframe.to_excel(writer, sheet_name="Sheet1", index=False)

dataframe = DataFrame({'ID': [1, 2, 3, 4, 5, 6, 7],
            'text_data': ['text1', 'text2', 'text3', 'text4', 'text5', 'text6', 'text7'],
            'number_data': [1, 2, 3, 4, 5, 6, 7,],
            'date_data': [Timestamp('2001-01-01 00:00:00'), Timestamp('2002-02-02 00:00:00'), Timestamp('2003-03-03 00:00:00'), Timestamp('2004-04-04 00:00:00'), Timestamp('2005-05-05 00:00:00'), Timestamp('2006-06-06 00:00:00'), Timestamp('2007-07-07 00:00:00')]})

            
Insert_Dataframe_Data_Into_Template(dataframe)

Here is template_files\example_template.xlsx. It is not visible, but the top row has bold text, and all digits have 2 decimals (except for ID) and all dates have DD/MM/YYYY format :

example template

Here is what I'm expecting in output_file.xlsx :

expected output

Here is what actually is output_file.xlsx (I extended the D column a bit to show that it isn't in the right format, but it's the same size as the others):

received output

How do I actually use a template file with styleframe.StyleFrame.read_excel_file_as_template ?


Solution

  • I found what caused the issue - styleframe seems to only read the format of a cell if this cell actually has a value. I just filled my template file with As, and everything worked as intended.

    the template file filled with the letter A in each cell