Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Excel file with multiple sheets into multiple csv files using Jupyter notebook

I have an excel file that contains multiple sheets. I want to convert these sheets into separate CSV files.

I tried this code and got an ordered dictionary of the sheets. Now I need to save them as CSV files in one step, instead of having to save each one manually in a separate step

xls = pd.ExcelFile('file.xlsx')
sheets = {}
for sheet_name in xls.sheet_names:
    sheets[sheet_name] = xls.parse(sheet_name)
like image 393
Michael Avatar asked Dec 05 '25 14:12

Michael


1 Answers

You can use to_csv to save dataframe as csv file:

# I prefer reading excel with pd.read_excel
# passing `sheet_name=None` returns a dictionary 
# with the form {sheet_name: dataframe}
data = pd.read_excel('file.xlsx', sheet_name=None)

# loop through the dictionary and save csv
for sheet_name, df in data.items():
    df.to_csv(f'{sheet_name}.csv')
like image 111
Quang Hoang Avatar answered Dec 08 '25 05:12

Quang Hoang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!