views:

172

answers:

1

I'm writing an Excel spreadsheet with Python's xlwt and I need numbers to be formatted using "." as thousands separator, as it is in brazilian portuguese language.

I have tried:

style.num_format_str = r'#,##0'

And it sets the thousands separator as ','. If I try setting num_format_str to '#.##0', I'll get number formatted as 1234.000 instead of 1.234. And if I open document in OpenOffice and format cells, I can set the language of the cell to "Portuguese (Brazil)" and then OpenOffice will show the format code as being "#.##0", but I don't find a way to set the cell's language to brazilian portuguese.

Any ideas?

+1  A: 

The thousands separator (and the decimal "point" etc) are recorded in the XLS file in a locale-independent fashion. The recorded thousands separator is a comma. How it is displayed depends on the user's locale. OpenOffice calc allows the user to override the default locale (Tools / Options / Languages / Locale setting).

With xlwt, write your num_format_str as "#,###.00" and your data as float("1234567.89"), like this:

import xlwt
b = xlwt.Workbook()
s = b.add_sheet('x')
style = xlwt.easyxf("", "#,###.00")
s.write(0, 0, 1234567.89, style)
b.save("locale_fmt_demo.xls")

Open the output file with OO Calc and try various locale settings. I got these results:

English (Australia): 1,234,567.89  
Portuguese (Brazil): 1.234.567,89
French (France):     1 234 567,89
John Machin
Hmmm, I see. Looks like in OpenOffice I can have a different locale for each cell, but not in Excel. I made a document with three cells with same number and format but different locales and saved in both .ods and .xls format. The .ods keeps the locale for each cell, but the .xls will have the same locale for all cells.
lfagundes
"the .xls will have the same locale for all cells" -- NO, as I said, the formatting is recorded in a locale-independent fashion. The file doesn't "have" a locale. Excel displays the file acording to the USER's locale.
John Machin