在使用XlsxWriter导出到pandas中的'xlsx'时应用样式 [英] Apply styles while exporting to 'xlsx' in pandas with XlsxWriter
问题描述
我使用pandas的.to_excel方法将DataFrame写为Excel工作簿。
当索引单元合并时,即使对于多索引DataFrame也适用。
当使用纯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.
但是我找不到用大熊猫做同样的方法方法。只是传递一个带有列名和样式的字典将是最直观的。
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.
有没有办法这样做?
推荐答案
有没有办法这样做
Is there any way to do so
目前没有。在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屋!