produce dataframe of Count no. of sheets in all workbooks in a directory

Question:

path =r"C:UsersAyushDesktopNew folder"

filenames = glob.glob(path + "*.xlsx")
print('File names:', filenames)

xl = pd.ExcelFile('C:\Users\Ayush\Desktop\New folder\1.xlsx')
res = len(xl.sheet_names)
print(res)

for file in filenames
xl1 = pd.ExcelFile(filenames)
res = len(xl1.sheet_names)
print(res)
Asked By: Ayush

||

Answers:

Try this…

import pandas as pd
from os import listdir
from os.path import isfile, join

mypath = r"C:Users"
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

filename, cnt, sheets = [],[],[]
for file in onlyfiles:
    if ".xlsx" in file:
        df = pd.ExcelFile(mypath+"\"+file)
        filename.append(file)
        cnt.append(len(df.sheet_names))
        sheets.append(df.sheet_names)
df_final = pd.DataFrame({"filename":filename,"sheets cnt":cnt,"sheets name":sheets})

Hope this helps…

Answered By: Sachin Kohli
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.