How to automatically adjust Excel column widths in openpyxl

This article was first published on Stringfest Analytics , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

Excel’s popularity stems from its user-friendly data formatting and presentation capabilities. Take, for example, the nuisance of narrow columns. Resolving this issue in Excel is as simple as selecting the affected columns and double-clicking on any one of them:

Python does present unique strengths to Excel, but visual formatting of the data can be difficult. It’s still, however, possible with the help of openpyxl:

Open the autofit_example.xlsx workbook created in the previous notebook and you’ll see all the content in the columns is easily visible:

What Excel automation tasks have you found particularly difficult in Python? Have you thought about defining functions for any other typical tasks? Let me know in the comments.

To leave a comment for the author, please follow the link and comment on their blog: Stringfest Analytics .

Want to share your content on python-bloggers? click here.