Django/python and isues with xlwt locale formatting -



Django/python and isues with xlwt locale formatting -

i using django-excel-view uses django-excel-response in turn uses xlwt. have scenario users can switch locale lets them view floats standard decimal point or in languages comma decimal point.

when exporting view xls standard decimal point locale works fine, using comma decimals makes xls file store float text , adds apostrophe before number (e.g. '123,45). have feeling excelresponse (https://djangosnippets.org/snippets/1151/) not handling float correctly (see line 43 onwards of snippet).

what right xlwt style apply xls saved correctly comma decimals , way check whether value comma decimal , should have style applied? in other words:

styles = {'datetime': xlwt.easyxf(num_format_str='yyyy-mm-dd hh:mm:ss'), 'date': xlwt.easyxf(num_format_str='yyyy-mm-dd'), 'time': xlwt.easyxf(num_format_str='hh:mm:ss'), 'default': xlwt.style.default_style, 'comma_decimal': xlwt.easyxf('????????')} rowx, row in enumerate(data): colx, value in enumerate(row): if isinstance(value, datetime.datetime): cell_style = styles['datetime'] elif isinstance(value, datetime.date): cell_style = styles['date'] elif isinstance(value, datetime.time): cell_style = styles['time'] elif isinstance(value, ?????????????): cell_style = styles['comma_decimal'] else: cell_style = styles['default'] sheet.write(rowx, colx, value, style=cell_style)

solution:

i ended adding check in django-excel-response regex check comma float values (which added django locale should be) , replaces commas decimal points.

elif (re.compile("^[0-9]+([,][0-9]+)?$")).match(u"{}".format(value)): value = float(value.replace(',', '.'))

jmcnamara helped point me in direction instead of messing locales , xlwt formatting.

i have scenario users can switch locale lets them view floats standard decimal point or in languages comma decimal point.

the of import thing note here the thousands/decimal separators locale windows setting , not excel setting. set in command panel/regional , language options/format or similar.

so if in 1 locale number format displayed in excel 1,234.56 , in locale displayed 1.234,56 in both cases the format stored in excel style: 0,000.00.

so, when using xlwt, or other spreadsheet writing module, should utilize style formatting options (comma one thousand , . decimal). displayed in users locale according windows settings.

python django excel xlwt

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -