python - 使用 xlsxwriter 在条件格式(公式)后删除列

标签 python pandas xlsxwriter

我有一个列(列 V),我曾经使用引擎 xlsxwriter 有条件地格式化另一列(列 U)。

所以我有这个:

# Light yellow fill with dark yellow text.
format1 = workbook.add_format({'bg_color':   '#FFEB9C'})

# Light red fill with dark red text.
format2 = workbook.add_format({'bg_color':   '#FFC7CE',
                           'font_color': '#9C0006'})


worksheet.conditional_format('U2:U1000', {'type': 'formula', 
                                 'criteria': '=V2>25',
                                 'format': format1})

worksheet.conditional_format('U2:U1000', {'type': 'formula',  
                                 'criteria': '=V2<-20',
                                 'format': format2})

所以现在在使用条件格式突出显示列 U 之后,我想删除列 V(但保持突出显示不变)。
有没有办法在 xlsxwriter 中做到这一点?

最佳答案

因为它是使用公式的条件格式,删除引用的列将删除引用的内容并“破坏”公式。

您可以使用以下代码隐藏列 V。
worksheet.set_column('V:V', None, None, {'hidden': True})
如果您需要隐藏单列,则需要将其设置为 'V:V'

http://xlsxwriter.readthedocs.io/example_hide_row_col.html

如果我们谈论的是数字公式,我会复制然后“粘贴值”以删除公式。据我所知,Excel 中没有办法在不复制条件公式的情况下复制和粘贴格式。

关于python - 使用 xlsxwriter 在条件格式(公式)后删除列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51111706/

相关文章:

python - 更新 Python 列表理解中的 JSON 元素

python - Pandas 在正则表达式上 split

python - pandas xlsxwriter,格式表头 - 不是表头

Python Excel 运行宏到 xlsx 文件

python - Python 中的 `write_formula` 中的 `xlsxwriter`

python - 在 python 记录器中执行相当于 log_struct 的操作

python - 使用对特定键值的理解从字典列表创建列表

python - 特威通 : Filtering more than one track.

python - 在 pandas 中 groupby 之后对数据框变量执行数学运算并将结果返回到原始数据框

python - 使用正则表达式将数据提取到 Pandas 中的不同列