5

使用 xlsxwriter 时,如何让 xlsx 文件在一列中包含带有逗号格式数字(不是字符串)的列?

我想要做的 -将 1000 变成 1,000,同时将值保留为数字

尝试失败...

#gives warning in cell (asking if it should be a number) + writes as string not number
sheet.write_string(0,0,re.sub("^0*|\.?0*$","",format(val, ',.10f')) )

# writes as number with no warning, but no commas
sheet.write_number(0,0,1000) 
4

1 回答 1

9

您需要设置单元格的格式以指定数字的表示方式:

num_fmt = workbook.add_format({'num_format': '#,###'})
...
sheet.write_number(0, 0, 1000, num_fmt)
于 2014-05-12T17:26:50.323 回答