数字格式在xlsxwriter中无法正常工作

问题描述 投票:1回答: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类的实例,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类的实例。

而且我正在按预期方式在前4个单元中取整,但没有在最后一个单元中取整(总计)。

是xlsxwriter的错误,还是我做错了什么?我该怎么做才能使我的整个单元格也四舍五入?

对不起,我的英语不好,谢谢你!

python xlsxwriter
1个回答
0
投票

我发现这个issue,如果简短,解决方法是使用1(作为数字)而不是'0':

© www.soinside.com 2019 - 2024. All rights reserved.