0

This is in reference to the question: "How to copy over an Excel sheet to another workbook in Python".

from win32com.client import Dispatch
path1 = 'C:\\Users\\Xukrao\\Desktop\\workbook1.xlsx'
path2 = 'C:\\Users\\Xukrao\\Desktop\\workbook2.xlsx'

xl = Dispatch("Excel.Application")
xl.Visible = True  # You can remove this line if you don't want the Excel 
application to be visible

wb1 = xl.Workbooks.Open(Filename=path1)
wb2 = xl.Workbooks.Open(Filename=path2)

ws1 = wb1.Worksheets(1)
ws1.Copy(Before=wb2.Worksheets(1))

wb2.Close(SaveChanges=True)
xl.Quit()
user3477363
  • 11
  • 1
  • 4

1 Answers1

0

Don't know if it's still needed, but in this line:

ws1.Copy(Before=wb2.Worksheets(1))

you can also use After to set your desired sheet location. See here for more details.

To place it at the end of all existing sheets, you may want to count the existing sheets first and use the result number to specify the last sheet of your file, for example:

wb2_lastsheet = len(wb2.Worksheets)
ws1.Copy(After=wb2.Worksheets(wb2_lastsheet))

Hope that helps!

xph
  • 937
  • 3
  • 8
  • 16