在 xlswriter 中,定义格式后,如何将其应用于范围而不是整列或整行?
例如:
perc_fmt = workbook.add_format({'num_format': '0.00%','align': 'center'})
worksheet.set_column('B:B', 10.00, perc_fmt)
这会应用于整个“B”列,但是这个“perc_fmt”如何应用于一个范围,例如,如果我这样做:
range2 = "B2:C15"
worksheet2.write(range2, perc_fmt)
它说:
TypeError: Unsupported type <class 'xlsxwriter.format.Format'> in write()
最佳答案
实际上我找到了一个避免执行循环的解决方法。 您只需要使用条件格式(将范围作为输入)并只格式化所有案例。例如:
worksheet2.conditional_format(color_range2, {'type': 'cell',
'criteria': '>=',
'value': 0, 'format': perc_fmt})
worksheet2.conditional_format(color_range2, {'type': 'cell',
'criteria': '<',
'value': 0, 'format': perc_fmt})
关于python - xlsxwriter 格式化范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34026362/