直接to_excel会被覆盖,借助ExcelWriter可以实现写多个sheet。
1
2
3
4
5
6
7
8
9
10
11
12
|
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的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/unsterbliche/article/details/80578606