Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy whole worksheet with openpyxl

Tags:

openpyxl

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.

like image 719
ralf.w. Avatar asked Nov 24 '14 08:11

ralf.w.


People also ask

How do you copy an Excel spreadsheet in Python?

Step1: Import the openpyxl library. Step2: Connect/load the Excel file to the program. Step3: Initialize variables with the source sheet name and destination sheet name. Step4: Create two variables to initialize with the total number of rows and columns in the source Excel sheet.

Does pandas work with openpyxl?

openpyxl has builtin support for the NumPy types float, integer and boolean. DateTimes are supported using the Pandas' Timestamp type.


1 Answers

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

like image 195
psychok7 Avatar answered Oct 09 '22 14:10

psychok7