[英]openpyxl add worksheet to workbook
Is it possible to add an existing worksheet object to a workbook object in openpyxl?是否可以将现有工作表 object 添加到 openpyxl 中的工作簿 object?
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.
因为像styles这样的东西是同一个工作簿中的不同工作表共享的,所以这些都需要工作簿来管理。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.