How can i convert multiple excel files with multiple sheets in an excel workbook to csv files in python?

Viewed 51

I have about 65 excel files some have several sheets on them i want to read and convert all them to csv and have each sheet have tittle of the excel concatenated with the name of the sheet so far I have read and added all the excel to a list but struggling converting to csv below is what I have so far

how can i proceed to convert these excel to csv files with their respective sheets?

import pandas as pd
import os 

list_of_names = os.listdir(path)

dataframes_list = [ ]

for file in list_of_names:

    if file.endswith('.xlsx'):
        df1 = pd.read_excel(os.path.join(path,file),sheet_name=None)
    elif file.endswith('.xlsm'):
        df2 = pd.read_excel(os.path.join(path,file),sheet_name=None)
        dataframes_list.append(df2)
    elif file.endswith('.xls'):
        df3 = pd.read_excel(os.path.join(path,file),sheet_name=None)
        dataframes_list.append(df3)
    elif file.endswith('.csv'):
        df4 = pd.read_csv(os.path.join(path,file))
        dataframes_list.append(df4)

for item in dataframes_list: 
    item.to_csv()

i get an error AttributeError: 'dict' object has no attribute 'to_csv'

however this works but i want a dynamic code to save all of them once and use the name of the data

dataframes_list[2].to_csv('sample.csv')
0 Answers
Related