Django / python and xlwt locale formatting problems

I use django-excel-view, which uses django-excel-response, which in turn uses xlwt. I have a scenario in which users can switch locales, which allows them to view floating point numbers with a standard decimal point or some comma languages.

When exporting an xls presentation with the standard locale of the decimal point, this works just fine, but using decimal points causes the xls file to save the floating point number as text and appends an apostrophe in front of the number (e.g. 123.45). I have the feeling that ExcelResponse ( https://djangosnippets.org/snippets/1151/ ) does not correctly handle the float (see line 43 of the fragment below).

What would be the proper xlwt style to properly store xls with decimal points and what is a good way to check if the value is a decimal point and should this style be 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('????????')}

for rowx, row in enumerate(data):
    for 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)

DECISION:

django-excel-response, ( django, ), .

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

jmcnamara , xlwt.

+4
2

, , .

, / - Windows, Excel. / / .

, Excel 1,234.56, 1.234,56 Excel : 0,000.00.

, xlwt ( ). Windows.

+4

easyxf, xwlt, :

# You can change the format to more or less decimals
decimal_style.num_format_str = '0.0000'  


sheet.write(0, 0, 3.18526119, decimal_style)  # Example to write
sheet.write(0, 1, 1.11, decimal_style)  # Another example

easyxf, - :

decimal_style = easyxf(num_format_str='0.00')
sheet.write(0, 0, '999.99', decimal_style)

Python: xlwt

+2

Source: https://habr.com/ru/post/1545149/


All Articles