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

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