gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:22:05 25 4
gpt4 key购买 nike

我正在尝试为 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 的错误,还是我做错了什么?我应该怎样做才能使我的总单元格也变圆?

我很抱歉我的英语不好,提前谢谢!

最佳答案

我找到了这个issue ,简单来说,这是一个 xlsxwriter 错误,解决方法是使用 1(作为数字)而不是“0”:

total_num_format.set_num_format(1)

希望这会节省某人的时间

关于python - 数字格式在 xlsxwriter 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58098929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com