可以使用以下代码示例来解决这个问题:
首先,在Excel中定义一个主题颜色:
rgb_color = (255, 0, 0)
theme_color = 6 # 主题颜色的索引号,这里为6
wb = xlsxwriter.Workbook('example.xlsx')
worksheet = wb.add_worksheet()
theme = wb.add_format({'bg_color': 'rgb{}'.format(rgb_color)})
workbook.set_theme({'theme_color': theme_color, 'theme': {'bg1': theme}})
然后,使用set_bg_color()
方法以及前面定义的主题颜色来设置单元格的背景色:
cell_format = wb.add_format()
cell_format.set_bg_color('theme:{}'.format(theme_color))
worksheet.write('A1', 'Hello, World!', cell_format)
同样地,可以使用set_font_color()
方法来设置文本的颜色。