How to Auto-Adjust the Width of Excel Columns with Pandas ExcelWriter | Towards Data Science

One of the most frustrating things you possibly need to deal with is when generating an Excel file using Python, that contains numerous columns you are unable to read due to the short width of the columns. Ideally, you should deliver readable spreadsheets where all the columns are properly formatted so that they are readable.
In this article, we are going to explore quick and easy ways one can use for
Dynamically adjusting all column widths based on the length of the column name
Adjusting a specific column by using its name
Adjusting a specific column by using its index
Finally, we will also discuss how to fix one common problem that might occur when calling set_column method (AttributeError: 'Worksheet' object has no attribute 'set_column').
First, let’s create a pandas DataFrame that will reference in our examples in order to demonstrate the actions we intend to discuss in this article.
Now let’s try to write the pandas DataFrame we’ve just created to a csv file using ExcelWriter, as shown below (Note that if the below snippet fails with ModuleNotFoundError: No module named openpyxl all you need to do is to install the library by running pip install openpyxl):
And the output spreadsheet should look similar to the one shown below. As you can see, columns with longer names are cropped and the table in general looks awful. The problem would be even bigger if you had to deal with many such columns.
In the following sections we will explore a few possible ways one can use in order to automatically adjust the width of the columns so that the output table in a spreadsheet will be more readable.
In order to automatically adjust the width of columns based on their length, we just need to iterate over the columns and set the column width accordingly, as shown below:
Note: If the below snippet fails with the following AttributeError, head to the end of the article to see how you can quickly resolve this.
AttributeError: 'Worksheet' object has no attribute 'set_column'
Now the output pandas DataFrame in the Excel spreadsheet is way more readable and it definitely looks better. All columns are adjusted to the corresponding width that will make them fit into the space without being cropped.
Now there is a chance you may wish to manually adjust the width only for a specific column (or subset of columns). You can do so, by referencing the column name as shown in the snippet below. For the sake of this example, let’s assume that we want to adjust the width of column this_is_a_long_column_name to 30:
The output of the above snippet is shown below. As we can see, the width of the column this_is_a_long_column_name has been adjusted to 20, while the remaining columns’ width were adjusted to the default value which makes columns with longer width (such as the last one) to be cropped.
Alternatively, you may wish to manually adjust the width of a specific column, by directly referencing its index. In the example shown below, we demonstrate this by adjusting the width of the last column.
Again, we can see that in this case the last column has been adjusted to width=40, so that it is wide enough to fit the column name.
In case any of the above operation fails with the error shown below
AttributeError: 'Worksheet' object has no attribute 'set_column'
all you need to do is install xlswriter
pip install xlsxwriter
In this article, we explore a few possible ways one can use to automatically adjust the columns’ width when writing a pandas DataFrame into Excel spreadsheets. We usually create spreadsheets so that we can generate pieces of information that look nice and are easy to read. Therefore, it is important to create spreadsheets that won’t require any manual effort from the reader to make them readable. You can achieve this with minimal code I’ve shared earlier that will definitely help you create high-quality excel files when attempting to create them out of pandas DataFrames.