MS Excel allows you to adjust the height of the rows and width of the columns in the worksheets. In MS Excel, you can simply do it using the Cell Width and Row Height options in the Format menu of the Cells group. However, if you are a programmer, you may need to programmatically manipulate the height and width of rows and columns, respectively. Accordingly, in this article, you will learn how to adjust row height and column width in Excel in Python.
- Python Library to Adjust Row Height and Column Width in Excel
- Adjust Row Height in Excel in Python
- Adjust Column Width in Excel in Python
Adjust Excel Row Height and Column Width - Python Library
To set the height of rows and width of columns, we’ll use Aspose.Cells for Python. It is a powerful and feature-rich library that lets you create and manipulate Excel files seamlessly from within your Python applications. You can install the API using the following pip command.
pip install aspose-cells
Python Adjust Height of Row in Excel
The following are the steps to adjust the height of a row in an Excel file in Python.
- First, load the Excel file using the Workbook class.
- Then, access the desired worksheet by index using Workbook.getWorksheets().get(index) method.
- Get cells collection in an object using Worksheet.getCells() method.
- Call setRowHeight() method and pass the row’s index and height as parameters.
- Finally, save the updated Excel file using Workbook.save(string) method.
The following code sample shows how to set the row height in Excel in Python.
The following is the screenshot of the Excel worksheet after setting the row height.
Set Width of Column in Excel in Python
The following are the steps to set a column’s width in an Excel file in Python.
- First, load the Excel file using the Workbook class.
- Then, access the desired worksheet by index using Workbook.getWorksheets().get(index) method.
- Get cells collection using Worksheet.getCells() method.
- Call setColumnWidth() method and pass the column’s index and width as parameters.
- Finally, save the updated Excel file using Workbook.save(string) method.
The following code sample shows how to set the width of a column in an Excel file in Python.
The following is the screenshot of the Excel worksheet after adjusting the column width.
Set Row Height and Column Width in Excel - Get a Free License
You can get a free temporary license to set the row height and column width in Excel files without evaluation limitations.
Python Excel Library - Read More
You can explore more about the Python Excel API using the documentation. You can also have a look at the API references here.
Summing Up
In this article, you have learned how to adjust rows’ height and columns’ width in Excel using Python. We have demonstrated how to set a particular height for a row and width for a column with the help of code samples and screenshots.
Have a Question?
In case you would have any questions or queries, you can share them with us via our forum.