13steinj
13steinj

Reputation: 467

Python and openpyxl - Changing column width to fit?

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?

Upvotes: 4

Views: 9472

Answers (1)

Charlie Clark
Charlie Clark

Reputation: 19507

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.

Upvotes: 2

Related Questions