XLSXWriter不将格式应用于XLSX文件中的标头行,而不是将格式应用于空单元格

发布于 2025-02-12 06:00:51 字数 682 浏览 1 评论 0原文

我有一个XLSX文件,对于所有表格,我需要更改标头行的格式以对其应用背景颜色。

但是,当我将fomratting添加到行中时,而不是包含我数据的列,它将应用于所有其他空列。

这是我尝试的:

for sheet_name in xlwriter.sheets:
    ws = xlwriter.sheets[sheet_name]
    ws.freeze_panes(1, 0) # Freeze the first row.
    
    cell_format = workbook.add_format({'bg_color': 'yellow'})
    cell_format.set_bold()
    cell_format.set_font_color('red')
    cell_format.set_border(1)
    ws.set_row(0, cell_format = cell_format)

PS:我尝试了其他问题的解决方案,这些问题是我对这个问题的建议,但这些都不适合我。

I have a xlsx file, for all of its sheet I need to change the formatting of the header row to apply background color to it.

But when I am adding fomratting to the row, instead of the columns which contain my data, it gets applied to all other empty columns.

enter image description here

Here is what I have tried :

for sheet_name in xlwriter.sheets:
    ws = xlwriter.sheets[sheet_name]
    ws.freeze_panes(1, 0) # Freeze the first row.
    
    cell_format = workbook.add_format({'bg_color': 'yellow'})
    cell_format.set_bold()
    cell_format.set_font_color('red')
    cell_format.set_border(1)
    ws.set_row(0, cell_format = cell_format)

P.S : I have tried solution from other question which I was getting as suggestion for this question but none of that works for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春风十里 2025-02-19 06:00:54

我认为最好循环浏览列名并单独编写每个单元格,而不是set_row:

for col, val in enumerate(df.columns):
    ws.write(0, col, val, cell_format)

使用Styler有另一个选项,但我认为边框有一个错误。

I think it is better to loop through the column names and write each cell separately, instead of set_row:

for col, val in enumerate(df.columns):
    ws.write(0, col, val, cell_format)

There is another option using styler, but I think there's a bug with the border.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文