0

I want to produce a multisheet CSV file from a multisheet xlsx file. For that I wrote this code:

xls = xlrd.open_workbook(r'Smallys ORDER.xlsx', on_demand=True)
df_list = []

names = xls.sheet_names()
names.remove('EVENT')

for name in names:
    prod = pd.read_excel('Smallys ORDER.xlsx', name, index_col=None)
    prod.to_csv(name + '.csv', encoding='utf-8', index=False) 
    df_list.append(prod)

df_final = pd.DataFrame()  

for df in df_list:
    df_final.append(df)

df_final.to_csv('smallys.csv', encoding='utf-8', index=False)

It successfully converts the individual xlsx sheets to csv files. But cannot produce the multisheet csv.

this print(df_final) outputs this :

Empty DataFrame
Columns: []
Index: []

1 Answer 1

2

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.append.html mentions that the append method returns a new object. So it has to be stored into a variable. Since you are not storing, your df_final is always empty. Try adding df_final = df_final.append(df)

Sign up to request clarification or add additional context in comments.

2 Comments

It combines all the csv's into one csv file, one sheet. No multi-sheet csv produced.
@Debbie What exactly are you trying to achieve by a "multisheet" csv? The original "Smallys ORDER.xlsx" file is multisheet because of attributes special to .xlsx files. A .csv is simply a file full of data delimited by commas and has no support for multisheet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.