简体   繁体   中英

How to put in the thousands separator in xlsx using xlsxwriter module?

When using xlsxwriter how do you get the xlsx file to contain columns with comma formatted numbers(not strings) in a column?

What I want to do - turn 1000 into 1,000 while preserving value as a number

Failed attempts...

#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) 

You need to set the cell's format to specify how the number should be represented:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM