如何在 xlsxwriter 中对整个列使用色标

How to use color scale to the whole column in xlsxwriter

根据此示例 (http://xlsxwriter.readthedocs.io/working_with_conditional_formats.html),您可以在列上使用色标。

文档中的示例:

caption = 'Examples of color scales and data bars. Default colors.'

data = range(1, 13)
worksheet7.write('A1', caption)
worksheet7.write('B2', "3 Color Scale")

for row, row_data in enumerate(data):
    worksheet7.write(row + 2, 1, row_data)

worksheet7.conditional_format('B3:B14', {'type': '3_color_scale'})

您可以看到格式应用于 1 到 14 的某个 B 列范围。我想将格式应用于整个 B 列,而不管它的长度。

当我这样做时:

caption = 'Examples of color scales and data bars. Default colors.'

 data = range(1, 13)
 worksheet7.write('A1', caption)
 worksheet7.write('B2', "3 Color Scale")

 for row, row_data in enumerate(data):
     worksheet7.write(row + 2, 1, row_data)

 worksheet7.conditional_format('B:B', {'type': '3_color_scale'})
 # or this worksheet7.conditional_format('$B:$B', {'type': '3_color_scale'})

我收到以下错误:

File "build\bdist.win-amd64\egg\xlsxwriter\worksheet.py", line 85, in cell_wrapper
  File "build\bdist.win-amd64\egg\xlsxwriter\utility.py", line 108, in xl_cell_to_rowcol
AttributeError: 'NoneType' object has no attribute 'group'

执行此操作的正确方法是什么?

通常在 XlsxWriter 中,您可以将 2D 范围设置为最大列值以获得 Col:Col 样式范围。这也是(通常)在 Excel.

内部发生的事情

所以以下内容应该适用于您的情况:

worksheet7.conditional_format('B1:B1048576', {'type': '3_color_scale'})

# Or with row, col notation:
worksheet7.conditional_format(0, 1, 1048575, 1, {'type': '2_color_scale'})

在 Excel 范围内将显示为 B:B