数字格式在 xlsxwriter 中无法正常工作
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
是自定义函数:
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的bug,还是我哪里做错了?
我应该怎么做才能使我的总单元格也变圆?
抱歉我的英语不好,在此先感谢!
我发现了这个 issue,简而言之,这是一个 xlsxwriter 错误,解决方法是使用 1(作为数字)而不是“0”:
total_num_format.set_num_format(1)
希望,这会节省一些人的时间
我正在尝试为 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
是自定义函数:
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的bug,还是我哪里做错了? 我应该怎么做才能使我的总单元格也变圆?
抱歉我的英语不好,在此先感谢!
我发现了这个 issue,简而言之,这是一个 xlsxwriter 错误,解决方法是使用 1(作为数字)而不是“0”:
total_num_format.set_num_format(1)
希望,这会节省一些人的时间