简体   繁体   中英

openpyxl add worksheet to workbook

Is it possible to add an existing worksheet object to a workbook object in openpyxl?

For better understanding: I DONT want to add a new sheet like this:

workbook.create_sheet('new sheet')

Instead i want to "merge" two existing sheets:

second_sheet = openpyxl.worksheet.worksheet.Worksheet()
workbook.add_sheed(second_sheet)

If you look at the source code you'll see this is possible but not advisable. Because things like styles are shared by different worksheets in the same workbook, these need to be managed by the workbook. This is also why it is not possible to move or copy worksheets between workbooks.

As you would know if you'd tried your own code, you must provide a parent workbook when creating a worksheet:

wb = Workbook()
ws = Worksheet(wb, "Sheetname")
wb._add_sheet(ws) # private API so guarantee that this will always be possible

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM