While working on the spreadsheets, you may often need to adjust the rows’ height and columns’ width according to the length of the text. For such cases, MS Excel provides a feature of auto-fitting the size of the cells. However, as a programmer, you may have to enable this feature for the Excel files programmatically. Therefore, in this article, I’ll show you how to auto-fit rows and columns in Excel files in Python.
- Python Library for Auto-Fitting Rows and Columns in Excel
- Auto-Fit Rows in Excel in Python
- Auto-Fit Columns in Excel in Python
Python Library to Auto-Fit Rows and Columns in Excel – Free Download#
For auto-fitting rows and columns in Excel, we will use Aspose.Cells for Python. The library provides a rich set of features to generate and process spreadsheets. You can install the library using the following pip command.
pip install aspose-cells
Auto-Fit Rows in Excel in Python#
To auto-fit rows, you first need to get the reference of the worksheet and then enable auto-fitting for the desired rows using their index. The following are the steps to auto-fit Excel rows in Python.
- First, load the Excel file using Workbook class.
- Then, use Workbook.getWorksheets().get(index) method to get reference of the desired worksheet.
- Call Worksheet.autoFitRow(rowIndex) method to auto-fit a row using its index.
- Finally, save the updated Excel file using Workbook.save() method.
The following code sample shows how to auto-fit an Excel row in Python.
Auto-Fit Columns in Excel in Python#
Similar to rows, you can auto-fit columns using their index. The following are the steps to auto-fit a column in Excel using Python.
- First, load the Excel file using Workbook class.
- Then, use Workbook.getWorksheets().get(index) method to get reference of the desired worksheet.
- Auto-fit column with Worksheet.autoFitColumn(columnIndex) method.
- Finally, save the updated Excel file using Workbook.save() method.
The following code sample shows how to auto-fit a column in Excel in Python.
Python Library to Auto-Fit Excel Rows and Columns – Get a Free License#
You can get a free temporary license to auto-fit rows and columns in your Excel files without evaluation limitations.
Conclusion#
In this article, you have learned how to auto-fit rows and columns in Excel files in Python. The code samples have demonstrated how to enable auto-fitting for a specific row or column using its index programmatically.
Explore Aspose’ Spreadsheet Library for Python#
You can visit the documentation to explore other features of Aspose.Cells for Python. Also, you can share your questions or queries with us via our forum.
See Also#
- Convert TXT to JSON in Python
- Convert JSON Files to XLS/XLSX in Java
- Convert Excel XLS Files to XLSX in Python