0

So long story short: I'm trying to create a program that that loops through excel files stored locally on my computer and adds the sheets from each excel file into a master workbook.

NOTE: I don't want to use pandas dataframe.append because each sheet has unique information and I want to retain every sheet separately.

Any help would be greatly appreciated!

3
  • Yes. Create a dictionary to store each Excel file as an individual dataframe, then write each dataframe in the dictionary as a sheet within a single Excel file. Commented Aug 3, 2021 at 19:44
  • Will this work if the excel files have more than one sheets? Commented Aug 3, 2021 at 20:09
  • It will work, but may require exceptions/etc. to handle any special cases. Impossible to say how without having access to your data files. Commented Aug 3, 2021 at 20:44

3 Answers 3

1

I think using openpyxl would be the easiest solution. It'll let you read and write excel sheets, and even gives you the option of creating various sheets. Personally it's my favorite route whenever I'm working with excel.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use pandas, as it can import and export specific sheets from excel-files:

import pandas as pd

sheet1 = pd.read_excel(filepath=r'C:\xlsfile.xlsx', sheet_name='Sheet 1')
sheet2 = pd.read_excel(filepath=r'C:\xlsfile.xlsx', sheet_name='Sheet 2')
sheet3 = pd.read_excel(filepath=r'C:\anotherfile.xlsx', sheet_name='Sheet 1')
sheet4 = pd.read_excel(filepath=r'C:\athirdfile.xlsx', sheet_name='Sheet 1')

outputfile = r'C:\output.xlsx'

sheet1.to_excel(outputfile, sheet_name='Sheet 1-1')
sheet2.to_excel(outputfile, sheet_name='Sheet 1-2')
sheet3.to_excel(outputfile, sheet_name='Sheet 2-1')
sheet4.to_excel(outputfile, sheet_name='Sheet 3-1')

this reads 4 single sheets from 3 seperate files and creates a single output-file with 4 sheets.

Comments

0
You could do something along the lines: 

import pandas as pd # pandas module for dataframes
import os  #module to get working directory
import xlrd# module to open excel files
from openpyxl import load_workbook #other module to open excel files
gbl = globals()
cwd =os.getcwd() # get the path to the current locaction
folders =[x for x in os.listdir() if "." not in x] # check for the folder with all the excels
path=cwd+"\\{}".format(folders[0]) # get the path to that folder
# listado de curso 
files = os.listdir(path)    # path to files 
files = list(filter(lambda f: f.endswith('.xls'), files))+list(filter(lambda f: f.endswith('.xlsx'), files))
files = [x for x in files if "~$" not in x]
writer = pd.ExcelWriter('multiple.xlsx', engine='xlsxwriter')
for x in files:
    workbook = pd.read_excel(path+"/"+x,sheet_name = None)
    for i in workbook.keys():
        gbl["workbook"+x+i] = workbook[i].copy()
        print("workbook"+x+i)
        gbl["workbook"+x+i].to_excel(writer, sheet_name=x+i)
writer.save()

I have edited it so you can do it with excel files with different sheets.

1 Comment

I hope this Answers your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.