用于当一个文件夹内有多个excel表,并且每个excel 里有多个sheet表的情况
import os
import pandas as pd
n=0
l=[]
for file in os.walk( r'.\2020年' ): #确保文件夹内只有需要读取的excel
print(file)
for table in file[2]: #file[2]是索引对应内容,好奇的话print一下对比就好
path = file[0] + '/' + table
f=pd.ExcelFile(path) #选择读取的excel表
if len(f.sheet_names)>1: #判断是否有多个sheet表
for j in f.sheet_names:
data=f.parse(sheet_name=j)
l.append(data)
n=n+1
print('第'+str(n)+'个表已提取')
if len(f.sheet_names)==1:
data=f.parse()
l.append(data)
n=n+1
print('第'+str(n)+'个表已提取')
data_result = pd.concat(l).reset_index(drop=True)
参考: