【发布时间】:2021-10-19 20:55:42
【问题描述】:
我经常看到 worksheet.conditional_format(),但我如何进行无条件格式化?
我一直在使用一个几乎总是正确的条件... value!=2147483647(some value)
worksheet.conditional_format('B3:K12',
{'type':'cell', 'criteria':'!=', 'value':2147483647, 'format':my_format})
我看到一些函数似乎在您编写单元格时设置格式,但我使用的是 Pandas,希望我可以在运行 df.to_excel(..) 后在 DataFrame 或工作表上设置格式可以用conditional_format()
worksheet.set_row( row, height, cell_format, options)
worksheet.set_column(first, last, width, cell_format, options)
# But I want to set a subset not a whole row/column
worksheet.write (0, 0, 'Foo', cell_format)
worksheet.write_string(1, 0, 'Bar', cell_format)
worksheet.write_number(2, 0, 3, cell_format)
worksheet.write_blank (3, 0, '', cell_format)
# And I'm not looking forward to writing every value myself.
我认为我的问题很有价值,因为其他人一定也有这个问题。
但是我没有找到好的解决方案。
https://xlsxwriter.readthedocs.io/working_with_pandas.html
XlsxWriter 和 Pandas 几乎不支持格式化数据帧中的输出数据,除了默认格式,例如标题和索引单元格以及包含日期或日期时间的任何单元格。此外,无法格式化已应用默认格式的任何单元格。
#
如果您需要非常可控的数据帧输出格式,那么您最好直接使用 Xlsxwriter 处理从 Pandas 获取的原始数据。但是,一些格式选项是可用的。
https://www.ojdo.de/wp/2019/10/pandas-to-excel-with-openpyxl/
# 对于一般样式,必须单独遍历所有单元格
https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html
样式器创建一个 HTML 表格
我很失望。
【问题讨论】:
-
youtube.com/watch?v=ZXjHAxHyi9k Pandas 和 Python 的条件格式