Copy whole worksheet with openpyxl

ralf.w. picture ralf.w. · Nov 24, 2014 · Viewed 36.4k times · Source

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.

Answer

psychok7 picture psychok7 · Jun 21, 2016

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