fableb
fableb

Reputation: 71

pandas - python export as xls instead xlsx - ExcelWriter

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

Answers (3)

Ali Taheri
Ali Taheri

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

Julien Marrec
Julien Marrec

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

EdChum
EdChum

Reputation: 394389

You can use to_excel and pass the extension .xls as the file name:

df.to_excel(file_name_blah.xls)

pandas will use a different module to write the excel sheet out, note that it will require you to have the pre-requisite 3rd party module installed.

Upvotes: 3

Related Questions