Setting Column Width with OpenPyXL

I am using OpenPyXL to generate some Excel downloads from one of my Django apps. In general, I'm pretty happy with how it works. Setting a column width is easy. Figuring out how to do that from the documentation is hard. So here it is, just in case someone else needs to know (or me a week from now when I forget).

Assuming you have OpenPyXL installed, to set the width of Column A to 50:

from openpyxl import Workbook

wb = Workbook()
worksheet = wb.active
worksheet.column_dimensions['A'].width = 50

wb.save(filename="col_width.xlsx")

Comments

Comments powered by Disqus