Copiar Rangos de Excel

Introducción

En Excel, puede seleccionar un rango, copiarlo y luego pegarlo con opciones específicas en la misma hoja de cálculo, en otras hojas de cálculo o en otros archivos.

Copiar rangos utilizando la biblioteca Aspose.Cells para Python Excel

Aspose.Cells para Python via .NET proporciona algunas sobrecargas de métodos Range.copy para copiar el rango. Y Range.copy_style solo el estilo de copia del rango; Range.copy_data solo el valor de copia del rango

Copiar Rango

Creación de dos rangos: el rango de origen, el rango de destino, luego copiar el rango de origen al rango de destino con el método Range.copy.

Vea el siguiente código:

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)

Pegar Rango Con Opciones

Aspose.Cells para Python via .NET admite pegar el rango con un tipo específico.

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)

Solo copiar datos del rango

También puedes copiar los datos con el método Range.copy_data como en el siguiente código:

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)

Temas avanzados