Guest
Guest

Reputation: 33

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.

Upvotes: 3

Views: 8216

Answers (1)

jmcnamara
jmcnamara

Reputation: 41574

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.

Upvotes: 3

Related Questions