pandas xlsxwriter,格式头
问题描述
我正在使用 xlsxwriter 将 pandas DataFrame 保存为 to_excel.除了更改标题的字体外,我已经设法格式化所有数据(设置列宽、字体大小等),但我找不到这样做的方法.这是我的例子:
将 pandas 导入为 pd数据 = pd.DataFrame({'test_data': [1,2,3,4,5]})writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')data.to_excel(writer, sheet_name='test', index=False)工作簿 = writer.book工作表 = writer.sheets['test']font_fmt = workbook.add_format({'font_name': 'Arial', 'font_size': 10})header_fmt = workbook.add_format({'font_name': 'Arial', 'font_size': 10, 'bold': True})worksheet.set_column('A:A', None, font_fmt)worksheet.set_row(0,无,header_fmt)writer.save()
尝试设置标题格式的倒数第二行没有任何作用.
解决方案我认为你需要先重置默认标题样式,然后你可以更改它:
pd.core.format.header_style = 无
大家一起:
将 pandas 导入为 pd数据 = pd.DataFrame({'test_data': [1,2,3,4,5]})writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')pd.core.format.header_style = 无data.to_excel(writer, sheet_name='test', index=False)工作簿 = writer.book工作表 = writer.sheets['test']font_fmt = workbook.add_format({'font_name': 'Arial', 'font_size': 10})header_fmt = workbook.add_format({'font_name': 'Arial', 'font_size': 10, 'bold': True})worksheet.set_column('A:A', None, font_fmt)worksheet.set_row(0,无,header_fmt)writer.save()
由 jmcnamara 解释,谢谢:p>
在 Excel 中,单元格格式覆盖行格式覆盖列格式.pd.core.format.header_style
被转换为格式并应用于标题中的每个单元格.因此,默认值不能被 set_row()
覆盖.将 pd.core.format.header_style
设置为 None
意味着标题单元格没有用户定义的格式,因此它可以被 set_row() 覆盖
.
在版本 0.18.1
你必须改变
pd.core.format.header_style = 无
到:
pd.formats.format.header_style = 无
从 0.20 版开始,这又发生了变化
导入pandas.io.formats.excelpandas.io.formats.excel.header_style = 无
感谢 krvkir.
从 0.24 版开始,现在需要此功能
导入pandas.io.formats.excelpandas.io.formats.excel.ExcelFormatter.header_style = 无
感谢 克里斯·韦基奥.
I'm saving pandas DataFrame to_excel using xlsxwriter. I've managed to format all of my data (set column width, font size etc) except for changing header's font and I can't find the way to do it. Here's my example:
import pandas as pd
data = pd.DataFrame({'test_data': [1,2,3,4,5]})
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
data.to_excel(writer, sheet_name='test', index=False)
workbook = writer.book
worksheet = writer.sheets['test']
font_fmt = workbook.add_format({'font_name': 'Arial', 'font_size': 10})
header_fmt = workbook.add_format({'font_name': 'Arial', 'font_size': 10, 'bold': True})
worksheet.set_column('A:A', None, font_fmt)
worksheet.set_row(0, None, header_fmt)
writer.save()
The penultimate line that tries to set format for the header does nothing.
解决方案I think you need first reset default header style, then you can change it:
pd.core.format.header_style = None
All together:
import pandas as pd
data = pd.DataFrame({'test_data': [1,2,3,4,5]})
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
pd.core.format.header_style = None
data.to_excel(writer, sheet_name='test', index=False)
workbook = writer.book
worksheet = writer.sheets['test']
font_fmt = workbook.add_format({'font_name': 'Arial', 'font_size': 10})
header_fmt = workbook.add_format({'font_name': 'Arial', 'font_size': 10, 'bold': True})
worksheet.set_column('A:A', None, font_fmt)
worksheet.set_row(0, None, header_fmt)
writer.save()
Explaining by jmcnamara, thank you:
In Excel a cell format overrides a row format overrides a column format.The pd.core.format.header_style
is converted to a format and is applied to each cell in the header. As such the default cannot be overridden by set_row()
. Setting pd.core.format.header_style
to None
means that the header cells don't have a user defined format and thus it can be overridden by set_row()
.
EDIT: In version 0.18.1
you have to change
pd.core.format.header_style = None
to:
pd.formats.format.header_style = None
EDIT: from version 0.20 this changed again
import pandas.io.formats.excel
pandas.io.formats.excel.header_style = None
thanks krvkir.
EDIT: from version 0.24 this is now required
import pandas.io.formats.excel
pandas.io.formats.excel.ExcelFormatter.header_style = None
thanks Chris Vecchio.
相关文章