Copy whole worksheet with openpyxl

风格不统一 提交于 2019-11-26 09:51:36

问题


Please can someone give me an example, how to copy a whole worksheet with styles (from rows and columns) to a second worksheet in the same workbook ? (in a new workbook would also be possible)

Thank you.

P.S.: I tried to do a deepcopy, but that failed on saving changed data cells.
Purpose is: I try to fill some worksheets with my data and the first worksheet is my template.

I was successful in copying the values but only some styles. I am using the latest version of openpyxl, so please no 1.x methods.


回答1:


Version 2.4 will allow you to do this: copy_worksheet

>>> source = wb.active
>>> target = wb.copy_worksheet(source)

For older ones you can probably copy the source code from here

UPDATE: You can't simply graft this code into older versions of the library




回答2:


You can't do this easily. The best approach is probably the one described in bug 171




回答3:


I had the same problem. I solved using copy instead deepcopy. I found the solution on this site

I hope this works for you!



来源:https://stackoverflow.com/questions/27101024/copy-whole-worksheet-with-openpyxl

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