問題描述
我正在使用 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.
這篇關于pandas xlsxwriter,格式頭的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!