英文:
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.
我正在创建一个程序,它使用styleframe
模块中的read_excel_file_as_template
函数将经过模板文件样式化的pandas DataFrames插入到Excel文件中,然后将其作为附件发送电子邮件。
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.
这里的想法是有一个名为template_files
的文件夹,其中包含仅进行格式化和样式处理而没有数据的模板文件。然后,我们将使用read_excel_file_as_template
将DataFrame的数据插入到新的Excel文件中,使用模板文件的样式。
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 :
这是template_files\example_template.xlsx
。虽然看不到,但顶部一行文字是粗体的,所有数字都保留2位小数(除了ID
),所有日期都采用DD/MM/YYYY
格式:
Here is what I'm expecting in output_file.xlsx
:
这是我在output_file.xlsx
中期望看到的内容:
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):
这是实际的output_file.xlsx
(我稍微扩展了D
列以显示它的格式不正确,但与其他列的大小相同):
How do I actually use a template file with styleframe.StyleFrame.read_excel_file_as_template
?
我应该如何实际使用模板文件与styleframe.StyleFrame.read_excel_file_as_template
?
英文:
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 :
Here is what I'm expecting in output_file.xlsx
:
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):
How do I actually use a template file with styleframe.StyleFrame.read_excel_file_as_template
?
答案1
得分: 1
我找到了问题的原因 - styleframe似乎只读取单元格的格式,如果该单元格实际上有一个值。我只是在我的模板文件中填充了A,一切都按预期工作。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论