Python xlsxwriter:更改单个单元格突出显示颜色

Python xlsxwriter: Changing individual cell highlight colour

我正在 Python 使用 xlsxwriter 模块编写代码。 我的问题有点像这里的其他问题,但有一点不同。我想更改 Excel 中特定单元格的突出显示颜色。有些单元格已经输入了数据,我希望保留这些数据。

以下是我目前的做法:

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

我不知道如何指示我的程序突出显示特定单元格而不删除其内容。目前,我认为某种形式的迭代可以解决问题,但我担心丢失已经输入到某些单元格中的数据。

之前:

之后:

非常感谢任何帮助或提示。

提前致谢。

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

您不能*在 XlsxWriter 中单独编写格式和数据。写入数据时需要将单元格格式写入单元格。

(*)可以在写完后加上conditional formatting to a range of cells