使用 XlsxWriter 在 Pandas 中导出到“xlsx"时应用样式 [英] Apply styles while exporting to 'xlsx' in pandas with XlsxWriter
问题描述
我使用 pandas 的 .to_excel 方法将 DataFrame 编写为 Excel 工作簿.即使对于多索引数据帧,当索引单元格合并时,这也能很好地工作.使用纯 XlsxWriter 时,我可以将格式应用于单元格,这也很好用.
I use the .to_excel method of pandas to write a DataFrame as an Excel workbook. This works nice even for multi-index DataFrames as index cells become merged. When using the pure XlsxWriter I can apply formats to cells what also works nice.
但是我找不到用熊猫方法做同样的事情的方法.只传递一个带有列名和样式的 dict 是最直观的.
However I couldn't find a way to do the same with the pandas method. Just passing a dict with column names and styles would be most intuitive.
有什么办法吗?
推荐答案
有什么办法吗
目前没有.没有像 Pandas 那样的格式化机制来格式化 Excel 输出(除了一些硬编码格式).
Currently no. There isn't a formatting mechanism like that in Pandas for formatting the Excel output (apart from a few hard-coded formats).
但是,即使是 XlsxWriter 目前也不支持在添加数据后格式化单元格.它位于 TODO 列表.
However, even if it was XlsxWriter doesn't currently support formatting cells after data is added. It is on TODO list.
更新:
作为一种解决方法,我建议获取对基础工作簿和工作表的引用,并使用来自 Pandas 数据帧和 XlsxWriter 格式的相同数据覆盖您希望格式化的任何单元格.
As a workaround I recommend getting a reference to the underlying workbook and worksheet and overwriting any cells that you wish to be formatted with the same data from the Pandas dataframe and a XlsxWriter format.
请参阅使用 Python Pandas 和 XlsxWriter.
这篇关于使用 XlsxWriter 在 Pandas 中导出到“xlsx"时应用样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!