The Copy method in Excel’s Range object is a fundamental operation for duplicating cell contents, formatting, and formulas to another location within the same workbook or to a different workbook. In xlwings, this functionality is accessed through the api property, which provides direct access to the underlying Excel object model. This allows for precise control over the copy operation, including specifying a destination range.
Functionality
The primary purpose of the Copy method is to replicate a source range. It copies all elements: values, formulas, number formats, cell formatting (like borders and fill), and column widths/row heights if the entire column/row is selected. This is equivalent to performing a standard copy-paste operation in the Excel UI. The method can be used without a destination to copy to the Clipboard, or with a destination to paste directly.
Syntax
The xlwings API call follows the pattern of the Excel VBA Range.Copy method.
range_object.api.Copy(Destination)
range_object: Your xlwings Range object (e.g.,sheet.range('A1:B2'))..api: The gateway to the native Excel object model..Copy: The method itself.Destination(Optional): An xlwings Range object specifying the top-left cell of the destination range. If omitted, the range is copied to the Clipboard.
Parameter Details
| Parameter | Type | Description | Required |
|---|---|---|---|
Destination | xlwings Range Object | The target cell where the copied range will be pasted. Only the top-left cell needs to be specified; the paste area will match the size of the source range. | No |
Code Examples
- Copy to a specific destination (direct paste):
This is the most common use case. It copiesA1:D5fromSheet1and pastes it starting at cellF1on the same sheet.
import xlwings as xw
wb = xw.Book.active
sht = wb.sheets['Sheet1']
source_range = sht.range('A1:D5')
destination_cell = sht.range('F1')
source_range.api.Copy(Destination=destination_cell.api)
- Copy to the Clipboard:
By not providing aDestinationargument, the range is placed on the Windows/macOS Clipboard. Another application (like another Excel instance or Notepad) can then paste it.
import xlwings as xw
sht = xw.Book.active.sheets[0]
sht.range('A1:A10').api.Copy() # Data is now on the clipboard
- Copy between different worksheets:
This example copies data fromSheet1toSheet2, starting at cellA1on the target sheet.
import xlwings as xw
wb = xw.Book.active
source_sht = wb.sheets['Sheet1']
target_sht = wb.sheets['Sheet2']
source_sht.range('C10:F20').api.Copy(Destination=target_sht.range('A1').api)
- Copy using a Range object variable for the destination:
This demonstrates using a pre-defined Range object as the target, which can be useful in dynamic scripts.
import xlwings as xw
app = xw.App(visible=False)
wb = app.books.open('report.xlsx')
source = wb.sheets['Data'].range('Table1[#All]') # Copy an entire Table
target_start = wb.sheets['Summary'].range('B5')
source.api.Copy(Destination=target_start.api)
wb.save()
app.quit()
Leave a Reply