在应用xlsxwriter python工具包时,使用add_format创建自定义样式对象时,使用的方式错误。在使用过样式后,期望通过改变旧样式将得到新样式应用到后续编码中,但是对于xlsxwriter来说,对于一个单元格应用样式,不是看的当前样式的参数,而是取决于这个自定义样式最后成型时的参数。
xlsxwriter官方文档原文参考如下:
Each unique cell format in an XlsxWriter spreadsheet must have a corresponding Format object. It isn’t possible to use a Format with a write() method and then redefine it for use at a later stage. This is because a Format is applied to a cell not in its current state but in its final state.
Consider the following example:
format = workbook.add_format({'bold': True, 'font_color': 'red'})
worksheet.write('A1', 'Cell A1', format)
Modified parameter of the format Later…..
format.set_font_color('green')
worksheet.write('B1', 'Cell B1', format)
Cell A1 is assigned a format which initially has the font set to the color red. However, the color is subsequently set to green. When Excel displays Cell A1 it will display the final state of the Format which in this case will be the color green.