Вы можете попробовать это, чтобы прочитать все файлы Excel в каталоге, включая подпапки:
import pandas as pd
import xlrd
import os
# Your current directory (including python script & all excel files)
mydir = (os.getcwd()).replace('\\','/') + '/'
#Get all excel files include subdir
filelist=[]
for path, subdirs, files in os.walk(mydir):
for file in files:
if (file.endswith('.xlsx') or file.endswith('.xls') or file.endswith('.XLS')):
filelist.append(os.path.join(path, file))
number_of_files=len(filelist)
print(filelist)
# Read all excel files and save to dataframe (df[0] - df[x]),
# x is the number of excel files that have been read - 1
df=[]
for i in range(number_of_files):
try:
df.append(pd.read_excel(r''+filelist[i]))
except:
print('Empty Ecxcel File!')
print(df)
Вывод (в моем примере у меня есть 4 файла Excel, в которых 3 файла Excel хранят номер телефона и 1 файлпусто):
['D:/SOF/Book1.xlsx', 'D:/SOF/Book2.xlsx', 'D:/SOF/a\\New Text Document.xlsx', 'D:/SOF/subdir1\\Book3.xlsx']
Empty Ecxcel File!
[ Name Phone
0 alfa 82330403045
1 fafa 82330403046
2 albert 82330403047
3 john 82330403048,
Name PhoneCell
0 alfa 82330403049
1 fafa 82330403050
2 albert 82330403051
3 john 82330403052,
Name PhoneCell
0 alfa 82330403049
1 fafa 82330403050
2 albert 82330403051
3 john 82330403052]
Надеюсь, это поможет вам:)