<bdo id='oqL7W'></bdo><ul id='oqL7W'></ul>

    1. <i id='oqL7W'><tr id='oqL7W'><dt id='oqL7W'><q id='oqL7W'><span id='oqL7W'><b id='oqL7W'><form id='oqL7W'><ins id='oqL7W'></ins><ul id='oqL7W'></ul><sub id='oqL7W'></sub></form><legend id='oqL7W'></legend><bdo id='oqL7W'><pre id='oqL7W'><center id='oqL7W'></center></pre></bdo></b><th id='oqL7W'></th></span></q></dt></tr></i><div id='oqL7W'><tfoot id='oqL7W'></tfoot><dl id='oqL7W'><fieldset id='oqL7W'></fieldset></dl></div>

      <tfoot id='oqL7W'></tfoot>
      <legend id='oqL7W'><style id='oqL7W'><dir id='oqL7W'><q id='oqL7W'></q></dir></style></legend>

      <small id='oqL7W'></small><noframes id='oqL7W'>

    2. pandas xlsxwriter,格式头

      时间:2023-10-08

      <small id='XnofK'></small><noframes id='XnofK'>

              <tbody id='XnofK'></tbody>
            <legend id='XnofK'><style id='XnofK'><dir id='XnofK'><q id='XnofK'></q></dir></style></legend>
            • <bdo id='XnofK'></bdo><ul id='XnofK'></ul>

                <i id='XnofK'><tr id='XnofK'><dt id='XnofK'><q id='XnofK'><span id='XnofK'><b id='XnofK'><form id='XnofK'><ins id='XnofK'></ins><ul id='XnofK'></ul><sub id='XnofK'></sub></form><legend id='XnofK'></legend><bdo id='XnofK'><pre id='XnofK'><center id='XnofK'></center></pre></bdo></b><th id='XnofK'></th></span></q></dt></tr></i><div id='XnofK'><tfoot id='XnofK'></tfoot><dl id='XnofK'><fieldset id='XnofK'></fieldset></dl></div>
              • <tfoot id='XnofK'></tfoot>
                本文介绍了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.

                这篇关于pandas xlsxwriter,格式头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何格式化列表以在 python 的单独行上打印每个元 下一篇:python字符串格式调用函数

                相关文章

                最新文章

              • <i id='7nQYb'><tr id='7nQYb'><dt id='7nQYb'><q id='7nQYb'><span id='7nQYb'><b id='7nQYb'><form id='7nQYb'><ins id='7nQYb'></ins><ul id='7nQYb'></ul><sub id='7nQYb'></sub></form><legend id='7nQYb'></legend><bdo id='7nQYb'><pre id='7nQYb'><center id='7nQYb'></center></pre></bdo></b><th id='7nQYb'></th></span></q></dt></tr></i><div id='7nQYb'><tfoot id='7nQYb'></tfoot><dl id='7nQYb'><fieldset id='7nQYb'></fieldset></dl></div>
                • <bdo id='7nQYb'></bdo><ul id='7nQYb'></ul>

                <small id='7nQYb'></small><noframes id='7nQYb'>

              • <tfoot id='7nQYb'></tfoot>

                  1. <legend id='7nQYb'><style id='7nQYb'><dir id='7nQYb'><q id='7nQYb'></q></dir></style></legend>