save workbook float format comma instead of point

1,200 views
Skip to first unread message

peter....@gmail.com

unread,
Feb 19, 2016, 7:46:50 AM2/19/16
to openpyxl-users
Hello,I am using openpyxl on a Windows Client with a german Office Version.
When saving the workbook, float values are saved as text, because float format uses a Point to separate decimals.
Importing and changing locales only has an Impact on the possibility to Format, as I understood it.

Import locale
locale.setlocale(locale.LC_ALL, 'deu_deu')

c = 0.1
v = locale.format("%g", c)

would correct the Format to 0,1 but also changes the type to a string, which openpyxl obviously tells Excel . The number is saved as text.

I then tries to set he cell format  ,, but unfortunately that has no effect either.
ws.cell('%s%s'%(col, row)).number_format = '0.00'

Saving c also Ends up with the number formatted as text

Can you please tell me what I am not seeing?

Thx Peter

Charlie Clark

unread,
Feb 19, 2016, 8:04:06 AM2/19/16
to openpyx...@googlegroups.com
Am .02.2016, 13:46 Uhr, schrieb <peter....@gmail.com>:

> Hello,I am using openpyxl on a Windows Client with a german Office
> Version.

> When saving the workbook, float values are saved as text, because float
> format uses a Point to separate decimals.

Saved by openpyxl? Then they are saved as floats using "%f.16g" to be
precise.

> Importing and changing locales only has an Impact on the possibility to
> Format, as I understood it.
>
> Import locale
> locale.setlocale(locale.LC_ALL, 'deu_deu')

This is irrelevant. Whether pi appears as 3.14 or 3,14 in Excel is
entirely down to the number format applied. Excel defaults to using a
comma as the separator for floats in German.

> c = 0.1
> v = locale.format("%g", c)
>
> would correct the Format to 0,1 but also changes the type to a string,
> which openpyxl obviously tells Excel . The number is saved as text.

Er, yes. Don't do this.

> I then tries to set he cell format ,, but unfortunately that has no
> effect either.
> ws.cell('%s%s'%(col, row)).number_format = '0.00'

Don't create cell addresses yourself. Just use ws.cell(row=1, col=1) (for
A1)

> Saving c also Ends up with the number formatted as text
>
> Can you please tell me what I am not seeing?

That Excel's localisation is deliberately confusing?

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226

peter....@gmail.com

unread,
Mar 5, 2016, 1:59:22 PM3/5/16
to openpyxl-users
Finally I found the problem. Setting the cell value command was wrong - using 'value'... Which treats everything as string.

Charlie Clark

unread,
Mar 5, 2016, 3:52:23 PM3/5/16
to openpyx...@googlegroups.com
Am .03.2016, 19:59 Uhr, schrieb <peter....@gmail.com>:

> Finally I found the problem. Setting the cell value command was wrong -
> using 'value'... Which treats everything as string.

Glad to hear you got it working but this is nonsense.

c...@badehotels.com

unread,
Feb 5, 2018, 9:34:43 AM2/5/18
to openpyxl-users
Hello, So what was the correct way of doing that?
Reply all
Reply to author
Forward
0 new messages