Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can use openpyxl to transfer an excel range from one workbook to another but into a distinct range by following these steps:

  1. Open the source workbook using openpyxl:

    from openpyxl import load_workbook src_wb = load_workbook('source_workbook.xlsx') src_ws = src_wb['Sheet1'] # Replace 'Sheet1' with the name of the source worksheet 
  2. Select the range from the source worksheet that you want to transfer:

    src_range = src_ws['A1:C4'] # Replace 'A1:C4' with the address of the range you want to transfer 
  3. Open the destination workbook using openpyxl:

    dest_wb = load_workbook('destination_workbook.xlsx') dest_ws = dest_wb['Sheet1'] # Replace 'Sheet1' with the name of the destination worksheet 
  4. Select the destination range where you want to transfer the data:

    dest_range = dest_ws['A5:C8'] # Replace 'A5:C8' with the address of the destination range 
  5. Copy the source range to the destination range:

    for row in src_range: for cell in row: dest_ws[cell.coordinate].value = cell.value 
  6. Save the destination workbook:

    dest_wb.save('destination_workbook.xlsx') 

This should transfer the data from the source range to the destination range in the destination workbook.