import openpyxl
import pandas as pd
def save_to_excel_sheet(data,path_excel,sheet_name):
"""将DataFrame数据存入指定excel的指定表格名称"""
wb = openpyxl.load_workbook(path_excel)
writer = pd.ExcelWriter(path_excel, engine='openpyxl')
writer.book = wb
data.to_excel(writer, sheet_name=sheet_name)
writer.save()
writer.close()
if __name__=='__main__':
pd.DataFrame().to_excel('生成数据/表格.xlsx', index=False)
path_excel='生成数据/表格.xlsx'
sheet_name='表格1'
data=pd.DataFrame([1, 2, 3, 4, 5])
save_to_excel_sheet(data,path_excel,sheet_name)
|