***Update
Thank you all for your input! Mouwsy's code is able to merge all worksheets within a Excel Workbook but how would this be done if the input is multiple Excel Workbooks? The output file only seems to be created from one input file instead of going through the loop. For example if there's 3 Excel Workbooks as the Input, I'm trying to have it so the Output is one Workbook containing 3 Worksheets - each Worksheet being the merged data.
import xlwings as xw
import glob
import sys
folder = sys.argv[1]
inputFile = sys.argv[2]
outputFile = sys.argv[3]
path = r""+folder+""
excel_files = glob.glob(path + "*" + inputFile + "*")
with xw.App(visible=False) as app:
for excel_file in excel_files:
wb_init = xw.Book(excel_file)
wb_res = xw.Book()
sheets = wb_init.sheets
ws_res = wb_res.sheets[0]
for ws in sheets:
ws.used_range.copy()
ws_res.used_range[-1:,:].offset(row_offset=1).paste()
ws_res["1:1"].delete()
wb_res.save(outputFile)
wb_res.close(); wb_init.close()
*OLD I'm trying to Merge all Worksheets within the same Excel Workbook using Xlwings if anyone could please advise on how this could be done?
The code below is able to grab all worksheets and combine them into a created output file but the worksheet tabs remain separated instead of being merged.
import xlwings as xw
import glob
import sys
folder = sys.argv[1]
inputFile = sys.argv[2]
outputFile = sys.argv[3]
path = r""+folder+""
excel_files = glob.glob(path + "*" + inputFile + "*")
with xw.App(visible=False) as app:
combined_wb = app.books.add()
for excel_file in excel_files:
print(excel_file)
wb = app.books.open(excel_file)
for sheet in wb.sheets:
sheet.copy(after=combined_wb.sheets[0])
wb.close()
combined_wb.sheets[0].delete()
combined_wb.save(outputFile)
combined_wb.close()
