解决pandas .to_excel不覆盖已有sheet的问题

(编辑:jimmy 日期: 2026/4/23 浏览:2)

直接to_excel会被覆盖,借助ExcelWriter可以实现写多个sheet。

from openpyxl import load_workbook
excelWriter = pd.ExcelWriter(os.path.join(output_dir, 'datapoint_statistic.xlsx'),
            engine='openpyxl')
pd.DataFrame().to_excel(os.path.join( output_dir,'datapoint_statistic.xlsx'))
#excel必需已经存在,因此先建立一个空的sheet
_excelAddSheet(DataFrame, excelWriter, sheet_name)
 
def _excelAddSheet(self,dataframe,excelWriter,sheet_name):
  book = load_workbook(excelWriter.path)
  excelWriter.book = book
  dataframe.to_excel(excel_writer=excelWriter,sheet_name=sheet_name,index=None)
  excelWriter.close()

以上这篇解决pandas .to_excel不覆盖已有sheet的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。