如何使用xlsxwriter模块在xlsx中放入千位分隔符?

时间:2014-05-12 16:40:14

标签: python excel xlsxwriter

使用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) 

1 个答案:

答案 0 :(得分:5)

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

num_fmt = workbook.add_format({'num_format': '#,###'})
...
sheet.write_number(0, 0, 1000, num_fmt)