Copy Ranges of Excel
Introduction
In Excel, you can select a range, copy the range, then paste it with specific options to the same worksheet, other worksheets or other files.
Copy Ranges Using Aspose.Cells for Python Excel Library
Aspose.Cells for Python via .NET provides some overload Range.copy methods to copy the range. And Range.copy_style only the copy style of the range; Range.copy_data only the copy value of the range
Copy Range
Creating two ranges: the source range, the target range, then copying source range to target range with Range.copy method.
See the following code:
from aspose.cells import 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] | |
# Create a range of cells. | |
sourceRange = worksheet.cells.create_range("A1", "A2") | |
# Input some data with some formattings into | |
# A few cells in the range. | |
sourceRange.get(0, 0).put_value("Test") | |
sourceRange.get(1, 0).put_value("123") | |
# Create target range of cells. | |
targetRange = worksheet.cells.create_range("B1", "B2") | |
# Copy source range to target range in the same workhseet | |
targetRange.copy(sourceRange) | |
# Create target range of cells. | |
workbook.worksheets.add() | |
worksheet = workbook.worksheets[1] | |
targetRange = worksheet.cells.create_range("A1", "A2") | |
# Copy source range to target range in another workhseet | |
targetRange.copy(sourceRange) | |
# Copy to another workbook | |
anotherWorkbook = Workbook() | |
worksheet = workbook.worksheets[0] | |
targetRange = worksheet.cells.create_range("A1", "A2") | |
# Copy source range to target range in another workbook | |
targetRange.copy(sourceRange) |
Paste Range With Options
Aspose.Cells for Python via .NET supports pasting the range with specific type.
from aspose.cells import PasteOptions, PasteType, 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] | |
# Create a range of cells. | |
sourceRange = worksheet.cells.create_range("A1", "A2") | |
# Input some data with some formattings into | |
# A few cells in the range. | |
sourceRange.get(0, 0).put_value("Test") | |
sourceRange.get(1, 0).put_value("123") | |
# Create target range of cells. | |
targetRange = worksheet.cells.create_range("B1", "B2") | |
# Init paste options. | |
options = PasteOptions() | |
# Set paste type. | |
options.paste_type = PasteType.VALUES_AND_FORMATS | |
options.skip_blanks = True | |
# Copy source range to target range | |
targetRange.copy(sourceRange, options) |
Only Copy Data Of The Range
Also you can copy the data with Range.copy_data method as the following codes:
from aspose.cells import 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] | |
# Create a range of cells. | |
sourceRange = worksheet.cells.create_range("A1", "A2") | |
# Input some data with some formattings into | |
# A few cells in the range. | |
sourceRange.get(0, 0).put_value("Test") | |
sourceRange.get(1, 0).put_value("123") | |
# Create target range of cells. | |
targetRange = worksheet.cells.create_range("B1", "B2") | |
# Copy the data of source range to target range | |
targetRange.copy_data(sourceRange) |