Copy pasting multiple sheets into new workbook

為{幸葍}努か 提交于 2021-02-17 06:11:53

问题


The goal is to copy paste multiple existing sheets out of a workbook into a new workbook using xlwings. I have:

app = xw.App(visible=False)
book = xw.Book(path)

sheet_1 = book.sheets["Sheet1"]
sheet_2 = book.sheets["Sheet2"]

wb_res = xw.Book()
sheet_active = wb_res.sheets.active
sheet_1.api.Copy(Before=sheet_active)

This throws:

TypeError: Objects for SAFEARRAYS must be sequences (of sequences), or a buffer object.

Bonus question for the brave:
- How can I replace all formulas on a sheet for its value?

Thanks


回答1:


If you can use win32com, you can try this:

from win32com.client import Dispatch

path1 = 'workbook1.xlsx'
path2 = 'workbook2.xlsx'

xl = Dispatch("Excel.Application")

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()


来源:https://stackoverflow.com/questions/53943314/copy-pasting-multiple-sheets-into-new-workbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!