I have xlsx format spreadsheets. For each one I need to set the column width for all but one column to 25 and the width of the remaining column to 50. The unit of measurement is the idiosyncratic one Excel uses.
The spreadsheets are originally produced in Google sheets which has no way to do this so currently I have to download them in xlsx format and then email them to a friend who has Excel to have them fixed as I don't own a copy of Excel myself. This is clearly far from ideal.
Is it possible to set the column widths using openpyxl?
Set the width of a column by calling the Cells collection's setColumnWidth method. The setColumnWidth method takes the following parameters: Column index, the index of the column that you're changing the width of. Column width, the desired column width.
You can quickly adjust the column width to accommodate the longest entry. Select any column from the worksheet. From the Cells group on the Home tab, choose Format. Select AutoFit Column Width to set that column to auto-fit the width depending on the contents.
XlsxWriter vs openpyxl: What are the differences? Developers describe XlsxWriter as "A Python module for creating Excel XLSX files". A Python module for creating Excel XLSX files. On the other hand, openpyxl is detailed as "A Python library to read/write Excel 2010 xlsx/xlsm files".
Openpyxl column width
ws.column_dimensions['A'].width = 25
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With