While working with spreadsheet manipulation, you may often need to insert or delete rows and columns within your worksheets. In accordance with that, this article covers how to manipulate rows and columns in worksheets programmatically. Particularly, you will learn how to insert or delete rows and columns in an Excel worksheet in Python.
- Python API to Insert or Delete Rows and Columns in Excel
- Insert Rows in a Worksheet using Python
- Insert Columns in Excel Worksheet using Python
- Delete Rows in Excel Worksheet using Python
- Delete Columns in Excel Worksheet using Python
- Get a Free API License
Python Library to Insert or Delete Excel Rows and Columns
In order to insert or delete rows and columns in XLSX/XLS worksheets, we’ll use Aspose.Cells for Python via Java API. It is a powerful spreadsheet manipulation API that provides a wide range of features for Excel automation. You can install the API using the following pip command.
pip install aspose-cells
How to Insert Rows in Excel using Python
The following are the steps to insert rows in an Excel worksheet in Python.
- First, load the Excel file using the Workbook class.
- Access the desired worksheet by index using Workbook.getWorksheets().get(index) method.
- Insert rows using Worksheet.getCells().insertRows(rowIndex, totalRows) method in which the first parameter is the row index and the second parameter is the number of rows you want to insert.
- Finally, save the updated file using Workbook.save(string) method.
The following code sample shows how to insert rows in an Excel worksheet using Python.
How to Insert Columns in Excel in Python
The following are the steps to insert columns in Excel worksheet using Python.
- First, load the Excel file using the Workbook class.
- Access the desired worksheet by index using Workbook.getWorksheets().get(index) method.
- Insert columns using Worksheet.getCells().insertColumns(columnIndex, totalColumns) method in which the first parameter is the column index and the second parameter is the number of columns you want to insert.
- Finally, save the updated file using Workbook.save(string) method.
The following code sample shows how to insert columns in an Excel worksheet using Python.
Delete Rows in an Excel XLSX in Python
The following are the steps to delete rows from an Excel worksheet using Python.
- First, load the Excel file using the Workbook class.
- Access the desired worksheet by index using Workbook.getWorksheets().get(index) method.
- Delete rows using Worksheet.getCells().deleteRows(rowIndex, totalRows) method in which the first parameter is the row index and the second parameter is the number of rows you want to delete.
- Finally, save the updated file using Workbook.save(string) method.
The following code sample shows how to delete rows from an Excel worksheet in Python.
Delete Columns in Excel XLSX in Python
The following are the steps to delete columns from an Excel worksheet using Python.
- First, load the Excel file using the Workbook class.
- Access the desired worksheet by index using Workbook.getWorksheets().get(index) method.
- Delete columns using Worksheet.getCells().insertColumns(columnIndex, totalColumns, updateReference) method. The first parameter is the column index, the second parameter is the number of columns you want to delete and the third parameter indicates if references need to be updated in other worksheets.
- Finally, save the updated file using Workbook.save(string) method.
The following code sample shows how to delete columns from an Excel worksheet using Python.
Get a Free API License
You can get a free temporary license in order to use the API without evaluation limitations.
Conclusion
In this article, you have learned how to work with rows and columns in Excel. Particularly, you have seen how to insert or delete rows and columns in Excel worksheets using Python. In addition, you can explore more about the Python Excel API using the documentation. Furthermore, you can share your queries with us via our forum.