Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) 
like image 308
ZJS Avatar asked May 12 '14 16:05

ZJS


1 Answers

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)
like image 195
mata Avatar answered Nov 06 '22 00:11

mata