Each source file contains only one sheet. All I want is to combine these sheets into a single file. I suppose I'll have to use win32com.
Does anyone know how?
Update: the sheets to be combined have extensive conditional formatting that I'd like to keep. The following code could only combine them with all conditionally formatting information lost.
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')
Thank you both! After taking your advice and trying for 5 minutes, the following worked!
import win32com.client as win32
import os
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Add()
for f in [os.path.join(os.getcwd(), "CM1.xlsx"), os.path.join(os.getcwd(), "CM2.xlsx")]:
w = excel.Workbooks.Open(f)
w.Sheets(1).Copy(wb.Sheets(1))
wb.SaveAs(os.path.join(os.getcwd(), "CM.xlsx"))
excel.Application.Quit()
this will paste sheet 1 ot to_copy to sheet 1 of emptyWorkbook
empty_wb = xl.Workbooks.Open(util.getTestRessourcePath("emptyWorkbook.xlsx"))
tocopy_wb = xl.Workbooks.Open(util.getTestRessourcePath("toCopy.xls"))
tocopy_wb.Sheets(1).Cells.Copy()
empty_wb.Sheets(1).Paste(empty_wb.Sheets(1).Range("A1"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.