Ask Your Question
1

How can I use openpyxl in python to transfer an excel range from one workbook to another but into a distinct range?

asked 2022-04-08 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-03-03 18:00:00 +0000

scrum gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-04-08 11:00:00 +0000

Seen: 11 times

Last updated: Mar 03 '22