I'm trying to merge all worksheet tabs of each excel file within a provided file path into one worksheet. For Example if there's 5 Excel files with multiple amounts worksheet tabs, each Excel file now only contains one merged worksheet tab. I would like to append each of these merged worksheet tabs to a created output file.
My Code below is able to appended all worksheet tabs from the provided source files to an output file but I can't figure out how to merge the worksheet tabs from the source first first. Does anyone have an Idea how I can accomplish this? Please see my code below - Thanks!:
import glob
import os
import pandas as pd
import sys
import os.path
from openpyxl import load_workbook
from openpyxl import Workbook
#System Arguments
folder = sys.argv[1]
inputFile = sys.argv[2]
outputFile = sys.argv[3]
# specifying the path to xlsx files
path = r""+folder+""
#Create the new Excel Workbook with nameing convention provided by user
def create_file():
wb = Workbook()
wb.save(outputFile)
#Append the NEW or EXISTING Workbook with Input Files and Tabs to the already existing Excel File
def appened_file():
outputPath = outputFile
book = load_workbook(outputPath)
writer = pd.ExcelWriter(outputPath, engine = 'openpyxl', mode="a", if_sheet_exists="new")
writer.book = book
for filename in glob.glob(path + "*" + inputFile + "*"):
print(filename)
excel_file = pd.ExcelFile(filename)
(_, f_name) = os.path.split(filename)
(f_short_name, _) = os.path.splitext(f_name)
for sheet_name in excel_file.sheet_names:
df_excel = pd.read_excel(filename, sheet_name=sheet_name,engine='openpyxl')
df_newSheets = pd.DataFrame(df_excel)
df_newSheets.to_excel(writer, sheet_name, index=False)
writer.save()