0

I'm trying to move multiple worksheets from multiple workbooks to one workbook. I want each worksheet (i.e. their data, everything is hard coded) to be moved to predetermined worksheets (e.g. worksheet 'temp' from workbook1 to be moved to worksheet 'temp' in workbook2). Currently I am able to move a worksheet to a new workbook but it creates a new sheet each time.

So far I have been using openpyxl but I can't figure out how to reference the worksheets by name instead of position

import openpyxl as xl
path1 = 'workbook 1'
path2 = 'workbook 2'
wb1 = xl.load_workbook(filename = path1)
ws1 = wb1.worksheets[0]
wb2 = xl.load_workbook(filename = path2)
ws2 = wb2.get_sheet_name["NAME"]
for row in ws1:
    for cell in row:
        ws2[cell.coordinate].value=cell.value
wb2.save(path2)

When I try reference the sheet name in wb2 is where the code breaks down. I'm sure there's an easy fix to this but I can't seem to work it out

CypherX
  • 7,019
  • 3
  • 25
  • 37
  • "When I try reference the sheet name in wb2 is where the code breaks down" - perhaps because the sheet does not yet exist in wb2? Try something like `ws2 = wb2.create_sheet()` –  Nov 07 '19 at 01:31
  • Possible duplicate of [Copy worksheet from one workbook to another one using Openpyxl](https://stackoverflow.com/questions/42344041/copy-worksheet-from-one-workbook-to-another-one-using-openpyxl) – Charlie Clark Nov 07 '19 at 14:37

0 Answers0