Adjust Row Height and Column Width in Excel using Python

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 Excel Row Height and Column Width

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

Adjust Row Height in Excel in Python

The following are the steps to adjust row height 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 rows’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 row height.

Adjust Row Height in Excel using Python

Set Column Width 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 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 column’s width in an Excel file in Python.

The following is the screenshot of the Excel worksheet after adjusting the column width.

Adjust Column Width in Excel using Python

Python Excel Library to Set Row Height and Column Width - 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.

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. In addition, you can explore more about the Python Excel API using the documentation. You can also have a look at the API references here.

Have a Question?

In case you would have any questions or queries, you can share with us via our forum.

See Also