0

I am trying to extract all data from one excel file (data is spread into different sheets from the same file) into one new excel file

import pandas as pd
import glob as glob

appended_data = []
f_list = glob.glob(r'C:\Users\Sarah\Desktop\test\
               *.xlsx') 
for f in f_list: 
    data = pd.read_excel(f, sheet_name= ('May', 'April'))
    appended_data.append(data)
appended_data = pd.concat(appended_data)
appended_data.to_excel(r'C:\Users\Sarah\Desktop\test\appended.xlsx')

the problem is in the sheet_name if i try the code below :

data = pd.read_excel(f, sheet_name= [0,1,2,3])

i get a Value Error : No objects to concatenate

the code only works when i type the name of one sheet :

data = pd.read_excel(f, sheet_name="April")

But it doesn't work when i try the name of two sheets or more :

data = pd.read_excel(f, sheet_name= ('May', 'April'))

How to tell the code to loop through all the sheets? Thank you

2 Answers 2

2

you can read all sheets by providing sheet_name=None

dict_of_frames = pd.read_excel(f, sheet_name=None)

full example:

all_sheets = []
for f in glob.glob(r'C:\Users\Sarah\Desktop\test\*.xlsx'):
        all_sheets.extend(pd.read_excel(f, sheet_name=None).values())
data = pd.concat(all_sheets)
data.to_excel(r'C:\Users\Sarah\Desktop\test\appended.xlsx')
Sign up to request clarification or add additional context in comments.

2 Comments

same problem : No objects to concatenate
I think this error is coming from the pd.concat(appended_data) line. Can you validate that the appended_data list is not empty
1

Get all sheet names using pd.ExcelFile class

sheet_names = pd.ExcelFile(f).sheet_names

and iterate them via a generator

appended_data = pd.concat((pd.read_excel(f, sheet_name=s) for s in sheet_names))

Update with the context:

appended_data = pd.concat((pd.read_excel(f, sheet_name=None) for f in f_list))

2 Comments

it says that the sheet_name is not defined
Of course the sheet_names are specific for every file if you iterate files. Thus, you should (for example) save appended_data to a list, and then pd.concat that list again. Anyway the solution with sheet_names=None above is much simpler and elegant.

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.