Apkawa / xlsx2html

A simple export from xlsx format to html tables with keep cell formatting
MIT License
50 stars 35 forks source link

Numbers in "text" format not converted as intended #22

Open linM24 opened 3 years ago

linM24 commented 3 years ago

Hi, thank you for this great work!

While I was working with a few excel files, I found one formatting issue with the numbers that are in the "text" format.

For example, if I have a zip code cell 85043, which is in "text" format in excel. The output value would be 90000 after the conversion. The numbers seem to be "rounded up or down" in this case.

I think it may result from the following function in xlsx2html.format. In the above scenario, the cell_format is '@' instead of 'General'.

So I'm curious if there's any preferred way of handling this type of data? Modifying the if statement could be temporary fix, but I' afraid it might fail in some other situations. Any suggestions? Many thanks!

def format_cell(cell, locale=None, f_cell=None):
    value = cell.value
    formatted_value = value or ' '
    cell_format = cell.number_format
    if not cell_format:
        return format_hyperlink(formatted_value, cell.hyperlink)

    if isinstance(value, six.integer_types) or isinstance(value, float):
        if cell_format.lower() != 'general':
            locale = locale or LC_NUMERIC
            formatted_value = format_decimal(value, cell_format, locale=locale)

    ...
Jones14021 commented 1 week ago

I confirm this behavior. It only appears for "text" formatted cells.

from openpyxl.styles import NamedStyle
NamedStyle(name="text_style", number_format="@")