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)
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With