'want to add worksheet from one workbook in to another workbook using openpyxl
I am pretty sure there is an easier way than how i am doing it, I just have two workbooks and a single sheet in each work book,
does anyone have any any suggestions?
import openpyxl as xl
path1 = 'C:\\Users\\path\\test1.xlsx' path2 ='C:\\Users\\path\\test2.xlsx'
wb1 = xl.load_workbook(filename=path1) ws1 = wb1.worksheets[0]
wb2 = xl.load_workbook(filename=path2) ws2 =
wb2.create_sheet(ws1.title)
for row in ws1:
for cell in row:
ws2[cell.coordinate].value = cell.value
wb2.save(path2)
The above code works for me, however it does not copy the format. just the cell values. I need formatting of the reports to be the same.
Solution 1:[1]
Here:
from openpyxl import load_workbook
from openpyxl import Workbook
wb = load_workbook(path)
ws = wb.active
wb2 = Workbook() #or load_workbook
ws2 = wb2.active
ws2 = ws
Edit:
Above doesn’t copy anything, this link shows how Copy whole worksheet with openpyxl
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |