[英]Number format not working properly in xlsxwriter
我正在尝试为 2 个创建的 xlsxwriter 格式设置不带小数位的数字格式:
text_wrap = wb.add_format()
text_wrap.set_text_wrap()
text_wrap.set_align('vcenter')
text_wrap.set_font_name('Arial')
text_wrap.set_font_size(10)
num_format = copy_format(wb, text_wrap)
num_format.set_num_format('0')
total_num_format = copy_format(wb, text_wrap)
total_num_format.set_bg_color(bg_color)
total_num_format.set_num_format('0')
其中“wb”是 WorkBook class 的实例, copy_format
是自定义 function:
def copy_format(workbook, existing_format):
"""
This function should be deleted when all reports with xlsxwriter
will be rewritten to classes
Give a format you want to extend and a dict of the properties you
want to extend it with, and you get them returned in a single format
:rtype : xlsxwriter.format.Format
"""
new_dict = {}
for key, value in existing_format.__dict__.iteritems():
if value:
new_dict[key] = value
del new_dict['escapes']
return workbook.add_format(new_dict)
因此,我将 num_format 应用于其单元格中有公式的一列,而将 total_num_format 应用于具有该列的 SUM 的单元格:
ws.write(0, 2, '=B1-A1', num_format)
ws.write(1, 2, '=B2-A2', num_format)
ws.write(2, 2, '=B3-A3', num_format)
ws.write(3, 2, '=B4-A4', num_format)
ws.write(4, 2, '=SUM(C1:C4)', total_num_format)
其中“ws”是 WorkSheet class 的实例。
而且我按预期在前 4 个单元格中得到了四舍五入的结果,但在最后一个单元格中没有四舍五入(总计)。
是 xlsxwriter 的错误,还是我做错了什么? 我应该怎么做才能使我的总细胞也变圆?
我很抱歉我的英语不好,提前谢谢!
我发现了这个问题,如果简单地说,它是一个 xlsxwriter 错误,解决方法是使用 1(作为数字)而不是 '0':
total_num_format.set_num_format(1)
希望,这将节省某人的时间
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.