Copier des plages d Excel
Introduction
Dans Excel, vous pouvez sélectionner une plage, copier la plage, puis la coller avec des options spécifiques dans la même feuille de calcul, d’autres feuilles de calcul ou d’autres fichiers.
Copier des plages en utilisant la bibliothèque Aspose.Cells pour Python Excel
Aspose.Cells pour Python via .NET fournit des surcharges de méthodes Range.copy pour copier la plage. Et Range.copy_style seulement le style de copie de la plage; Range.copy_data seulement la valeur de copie de la plage
Copier la plage
Création de deux plages : la plage source, la plage cible, puis copie de la plage source vers la plage cible avec la méthode Range.copy.
Voir le code suivant :
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) |
Coller la plage avec des options
Aspose.Cells pour Python via .NET prend en charge le collage de la plage avec un type spécifique.
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) |
Copier uniquement les données de la plage
Vous pouvez également copier les données avec la méthode Range.copy_data comme dans les codes suivants:
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) |