Reputation: 71
I would like to export my pandas dataframe as a xls
file and not a xlsx
.
I use ExcelWriter.
I have done :
xlsxWriter = pd.ExcelWriter(str(outputName + "- Advanced.xls"))
Unfortunatly, nothing outputs.
I think I have to change the engine, but I don't know how?
Upvotes: 3
Views: 8333
Reputation: 146
The easiest way to do this is to install the "xlwt" package on your active Env.
pip install xlwt
then just simply use the below code:
df.to_excel('test.xls')
Upvotes: 0
Reputation: 11905
If for some reason you do need to explicitly call pd.ExcelWriter
, here's how:
outputName = "xxxx"
xlsWriter = pd.ExcelWriter(str(outputName + "- Advanced.xls"), engine = 'xlwt')
# Convert the dataframe to an Excel Writer object.
test.to_excel(xlsWriter, sheet_name='Sheet1')
# Close the Pandas Excel writer and output the Excel file.
xlsWriter.save()
It's critical not to forget the save()
command. That was your problem.
Note that you can also set the engine
directly like so: test.to_excel('test.xls', engine='xlwt')
Upvotes: 2