pandas – python导出为XLS而不是XLSX – ExcelWriter

我想将我的pandas数据框导出为xls文件而不是xlsx

我使用ExcelWriter。

我已经做好了 :

 xlsxWriter = pd.ExcelWriter(str(outputName + "- Advanced.xls")) 

不幸的是,没有输出。

我想我必须改变引擎,但我不知道如何?

您可以使用to_excel并传递扩展名.xls作为文件名:

 df.to_excel(file_name_blah.xls) 

pandas将使用不同的模块来写入Excel表格,请注意,它将要求您安装必要的第三方模块。

如果由于某种原因,您确实需要显式调用pd.ExcelWriter ,请执行以下操作:

 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() 

不要忘记save()命令。 那是你的问题。

请注意,您也可以直接设置engine ,如下所示: test.to_excel('test.xls', engine='xlwt')