简体   繁体   中英

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.

I used XlsxWriter and did conditional formatting to highlight the cells that contain "-->". 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. 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). 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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