简体   繁体   English

使用 Python - xlsxwriter 写入文件后,条件格式仍然适用。 有没有办法让它不起作用?

[英]The conditional format still applies after writing a file using Python - xlsxwriter. Is there a way to make this not work?

I compared two excel files and where ever there is change, I added "-->" to identify the change.我比较了两个 excel 文件,如果有变化,我添加了“-->”来识别变化。

I used XlsxWriter and did conditional formatting to highlight the cells that contain "-->".我使用 XlsxWriter 并进行条件格式化以突出显示包含“-->”的单元格。 I save and close the workbook.我保存并关闭工作簿。

Now, when I open the excel file from the folder that is saved and change "-->" to blank, the applied conditional formatting also disappears.现在,当我从保存的文件夹中打开 excel 文件并将“-->”更改为空白时,应用的条件格式也会消失。 But I want to keep the conditional formatting even after I remove "-->" value from the cell.但即使在我从单元格中删除“-->”值后,我也想保留条件格式。 Could someone help me pls?有人可以帮我吗?

Below is my code for conditional formatting下面是我的条件格式代码

worksheet.conditional_format(1,1,df.shape[0],df.shape[1],
                                            {'type': 'text',
                                            'criteria': 'containing',
                                            'value':' -->',
                                            'format': green_fmt})

I want to keep the conditional formatting even after I remove "-->" value from the cell即使从单元格中删除“-->”值,我也想保留条件格式

As far as I know that isn't possible in Excel (and therefore not in XlsxWriter).据我所知,这在 Excel 中是不可能的(因此在 XlsxWriter 中是不可能的)。 A conditional format is based on a condition.条件格式基于条件。 If you remove the condition you will turn off the formatting.如果您删除条件,您将关闭格式。

As an alternative you could iterate over the data in the DataFrame to look for matches and add cell formatting using to the matching cells using write() with a format.作为替代方案,您可以遍历 DataFrame 中的数据以查找匹配项并使用带有格式的write()将单元格格式添加到匹配的单元格中。

You could also use Pandas styling for this, although you will need to use openpyxl as the Excel engine to be able to export that.您也可以为此使用 Pandas 样式,尽管您需要使用 openpyxl 作为 Excel 引擎才能导出它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM