4

Say I had a workbook, and I wanted to make sure that all columns are readable; how would I determine the width the column needs to be from a cell's text, to then change the width of the column?

13steinj
  • 427
  • 2
  • 9
  • 16
  • Sadly, as noted by @Charlie Clark, the bestFit attribute is not that helpful and there is no method provided within the library to do this. There are [some answers here](https://stackoverflow.com/questions/39529662/python-automatically-adjust-width-of-an-excel-files-columns) for manual workarounds. โ€“ bigsee Dec 13 '18 at 15:11

1 Answers1

2

You can't really do this accurately: column widths are calculated using the typeface installed on the computer system with some padding. So you can only work with a best guess.

You can set the bestFit attribute for a column but this often doesn't do what you expect.

From the specification ยง 18.3.1.13:

'Best fit' is set to true under these conditions:

  • The column width has never been manually set by the user, AND
  • The column width is not the default width
  • 'Best fit' means that when numbers are typed into a cell contained in a 'best fit' column, the column width should automatically resize to display the number.
Charlie Clark
  • 18,477
  • 4
  • 49
  • 55
  • 2
    Can you provide a few lines of code to demonstrate? Does the variable width issue go away with a fixed width typeface? โ€“ flywire May 03 '18 at 12:55
  • @flywire check out the answers [here](https://stackoverflow.com/questions/39529662/python-automatically-adjust-width-of-an-excel-files-columns) if you haven't already... โ€“ bigsee Dec 13 '18 at 15:13