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

An alternative approach for auto column widths #155

Open
jtrim-ons opened this issue Jan 31, 2022 · 1 comment
Open

An alternative approach for auto column widths #155

jtrim-ons opened this issue Jan 31, 2022 · 1 comment
Assignees

Comments

@jtrim-ons
Copy link

jtrim-ons commented Jan 31, 2022

Is your feature request related to a problem? Please describe.
Automatically calculated column widths don't take account of numeric values in columns, and sometimes don't quite match the width of the widest text in a column.

Describe the solution you'd like
Auto column widths calculated by summing the widths of the Arial characters that make up a string.

Describe alternatives you've considered
I've tried to implement a prototype of this in the PR linked below.

I measured Arial characters using JavaScript, and modified the calculation function in wrappers.py to use these character widths.

I've also changed the code to measure widths of numbers, assuming no more than 3 decimal places. I thought this might be useful if a column contains big numbers but has a very short heading.

Calculating the width of a string by adding up the widths of individual characters isn't completely accurate. For example, the width of 'Ta' is less than the the sum of the widths of 'T' and 'a'. However, the simple approach seems to do a good job. If you wanted, there might be a really accurate way to measure string width by looking at the width of each character when it appears after each other possible character.

Another possibility would be to use Pillow to find text widths. I think this would be slower.

Additional context
N/A

@jtrim-ons jtrim-ons changed the title Improved auto column widths An alternative approach for auto column widths Jan 31, 2022
@ldavies99 ldavies99 self-assigned this Jul 14, 2023
@jtrim-ons
Copy link
Author

It looks like an AutoFit feature was added to XlsxWriter recently that uses a similar approach. I'm not sure if this works well enough to do the job? (It might have a hard-coded font and size, I think.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

2 participants