xlswriter格式化范围 [英] xlswriter formatting a range

查看:228
本文介绍了xlswriter格式化范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在xlswriter中,一旦定义了格式,如何将其应用于范围而不是整个列或整个行?

In xlswriter, once a format is defined, how can you apply it to a range and not to the whole column or the whole row?

例如:

perc_fmt = workbook.add_format({'num_format': '0.00%','align': 'center'})
worksheet.set_column('B:B', 10.00, perc_fmt)

这将其应用于整个"B"列,但是如何将此"perc_fmt"应用于某个范围,例如,如果我这样做:

this gets applied it to the whole "B" column, but how can this "perc_fmt" applied to a range, for example, if I do:

range2 = "B2:C15"
worksheet2.write(range2, perc_fmt)

它说:

TypeError: Unsupported type <class 'xlsxwriter.format.Format'> in write()

推荐答案

实际上,我发现了一种避免执行循环的解决方法. 您只需要使用条件格式(将范围作为输入)并格式化所有案例.例如:

Actually I found a workaround that avoids doing the loop. You just need to use the conditional formatting (that takes a range as an input) and just format all cases. For example:

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})  

这篇关于xlswriter格式化范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆