Delete column after conditional formatting (formula) using xlsxwriter
Asked Answered
S

1

5

I have a column (column V) that I used to conditionally format another column (column U) using engine xlsxwriter.

So I have this:

# 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})

So now after highlighting column U with conditional formatting, I want to delete column V (yet keep the highlighting intact). Is there a way to do this in xlsxwriter?

Sunsunbaked answered 30/6, 2018 at 4:23 Comment(0)
E
6

Because it's a conditional format using a formula, deleting the referenced column will remove what's being referenced and "break" the formula.

You could hide column V instead with the following code.

worksheet.set_column('V:V', None, None, {'hidden': True})

If you need to hide single column then you need to set it like 'V:V'

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

If we were talking about formulas for numbers, I would copy then "paste values" to remove the formulas. As far as I know, there isn't a way in Excel to copy and paste formats without also copying the conditional formula.

Echinus answered 30/6, 2018 at 6:13 Comment(1)
And for whatever reason worksheet.set_column('V:W', None, None, {'hidden' : True}) still only hides 'V'. I wrote two separate lines to hide two columns.Invisible

© 2022 - 2024 — McMap. All rights reserved.