Copy entire worksheet using openpyxl

Please give me an example of how to copy an entire worksheet with styles (from rows and columns) to a second worksheet in the same book? (in the new book it will also be possible)

Thanks.

PS: I tried to make a deep copy, but it was not possible to save the modified data cells.
Purpose: I'm trying to fill in some sheets with my data, and the first worksheet is my template.

I managed to copy the values , but only some styles . I am using the latest version of openpyxl, so please do not use the 1.x methods.

+7
openpyxl
source share
3 answers

Version 2.4 will allow you to do this: copy_worksheet

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

For older, you can copy the source code here

UPDATE: You cannot just port this code to old versions of the library

+8
source share

You cannot do it easily. The best approach is probably described in bug 171

+1
source share

I had the same problem. I decided to use a copy instead of deepcopy. I found a solution on this site

Hope this works for you!

0
source share

All Articles