Insert Ranges

Introduction

In Excel, you can select a range, then insert a range and shift other data right or down.

Shift options

Insert Ranges Using Aspose.Cells for Python Excel Library

Aspose.Cells for Python via .NET provides Cells.insert_range method to insert a range.

How to Insert Ranges And Shift Cells Right

Insert a ranage and shift cells right as the following codes with Aspose.Cells:

from aspose.cells import CellArea, ShiftType, Workbook
# Instantiate a new Workbook.
workbook = Workbook()
# Get all the worksheets in the book.
worksheets = workbook.worksheets
# Get the first worksheet in the worksheets collection.
worksheet = workbook.worksheets[0]
cells = worksheet.cells
# Create a range of cells.
sourceRange = cells.create_range("A1", "A2")
startRow = sourceRange.first_row
startCol = sourceRange.first_column
# Input some data with some formattings into
# A few cells in the range.
cells.get(startRow, startCol).put_value("Test")
cells.get(startRow + 1, startCol).put_value("123")
ca = CellArea.create_cell_area("A1", "A2")
worksheet.cells.insert_range(ca, ShiftType.RIGHT)
print(worksheet.cells.get("B1").string_value == "Test")

How to Insert Ranges And Shift Cells Down

Insert a ranage and shift cells down as the following codes with Aspose.Cells:

from aspose.cells import CellArea, ShiftType, Workbook
# Instantiate a new Workbook.
workbook = Workbook()
# Get all the worksheets in the book.
worksheets = workbook.worksheets
# Get the first worksheet in the worksheets collection.
worksheet = workbook.worksheets[0]
cells = worksheet.cells
# Create a range of cells.
sourceRange = cells.create_range("A1", "A2")
startRow = sourceRange.first_row
startCol = sourceRange.first_column
# Input some data with some formattings into
# A few cells in the range.
cells.get(startRow, startCol).put_value("Test")
cells.get(startRow + 1, startCol).put_value("123")
ca = CellArea.create_cell_area("A1", "A2")
worksheet.cells.insert_range(ca, ShiftType.DOWN)
print(worksheet.cells.get("A3").string_value == "Test")