Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write_excel(..., autofit=True) auto-fits to source, not formatted data #17853

Closed
2 tasks done
mcrumiller opened this issue Jul 24, 2024 · 2 comments
Closed
2 tasks done
Labels
A-io-spreadsheet Area: reading/writing Excel/ODS files bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars

Comments

@mcrumiller
Copy link
Contributor

mcrumiller commented Jul 24, 2024

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

import polars as pl

df = pl.DataFrame({
    "Text": ["Hi there", "longer sentence", "this is left aligned"],
    "Number": [1.0, 2.345, 0.398374937341998375913758438431441],
})

df.write_excel("out.xlsx", autofit=True)

image

Issue description

When using autofit, it appears the column width is calculated from the original number, not from the formatted result. In the example above, the column width is as it would be had we kept many decimals in the output format, and as a result, we have unused whitespace.

Expected behavior

Should fit to truncated, output-formatted decimal.

Installed versions

main

@mcrumiller mcrumiller added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Jul 24, 2024
@alexander-beedie
Copy link
Collaborator

alexander-beedie commented Jul 25, 2024

FYI: the behaviour of autofit is an XlsxWriter feature rather than a Polars feature - we just call "worksheet.autofit()" if it is requested. So, unless we want to write a custom version of that ourselves (and/or contribute enhancements upstream?), you'll need to make a feature request for improvements over there ;)

Note that "better autofit" is already mentioned in the XlsxWriter roadmap:
jmcnamara/XlsxWriter#1028

@alexander-beedie alexander-beedie added the A-io-spreadsheet Area: reading/writing Excel/ODS files label Jul 25, 2024
@mcrumiller
Copy link
Contributor Author

Thanks @alexander-beedie, definitely not polars fault!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-io-spreadsheet Area: reading/writing Excel/ODS files bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars
Projects
None yet
Development

No branches or pull requests

2 participants