Merging worksheets from different xlsx files into a single workbook and saving
Created originally on Bitbucket by pleopard777 (Paul Leopard)
Given several XLSX files, each containing a single worksheet, is it possible to use OpenPYXL to copy the worksheets from these files and combine into a single workbook with the latest release? Googling found this code on stackoverflow but it complains that Workbook has no method named add_sheet. I assume that add_sheet has been removed. Is it possible to resurrect add_sheet for an upcoming release?
from openpyxl import load_workbook, Workbook import os
fwb = Workbook()
wb = load_workbook('CM1.xlsx') ws1 = wb.active wb = load_workbook('CM2.xlsx') ws2 = wb.active wb = load_workbook('CM3.xlsx') ws3 = wb.active
fwb.add_sheet(ws1) fwb.add_sheet(ws2) fwb.add_sheet(ws3)
fwb.save('CM.xlsx')