如何将工作表复制到工作簿的末尾而不是开头?

2021-12-08 05:14:46 发布

您现在位置:Python中文网/ 问答频道 /正文

This is in reference to the question:“如何将Excel工作表复制到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()