Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python xlsxwriter: Changing individual cell highlight colour

I am writing code in Python using the xlsxwriter module. My problem is somewhat like other questions here, but with a small twist. I am wanting to change the highlight colour of SPECIFIC INDIVIDUAL cells in Excel. Some cells will already have data entered into them, and I wish to keep that data.

The following is my approach so far:

## Highlights appropriate cells
row = 0
col = 0

blue_fmt = workbook.add_format()
blue_fmt.set_pattern(1)
blue_fmt.set_bg_color('#0000FF')

yellow_fmt = workbook.add_format()
yellow_fmt.set_pattern(1)
yellow_fmt.set_bg_color('#FFFF00')

black_fmt = workbook.add_format()
black_fmt.set_pattern(1)
black_fmt.set_bg_color('#000000')

I am lost as to how I can instruct my program to highlight specific cells without removing its contents. At the moment, I am thinking some form of iteration would do the trick, but I am worried about losing data already entered into certain cells.

Before: enter image description here

After: enter image description here

Any help or hints are much appreciated.

Thanks in advance.

like image 755
Guest Avatar asked Jun 10 '26 12:06

Guest


1 Answers

how can I instruct my program to highlight specific cells without removing its contents.

You can't* write formatting and data separately in XlsxWriter. You will need to write the cell format to the cell when you write the data.

(*) It is possible to add conditional formatting to a range of cells after you have written them.

like image 143
jmcnamara Avatar answered Jun 12 '26 05:06

jmcnamara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!