我有一个模板文件Template.xlsx。它有 2 张:“每月保费”和“每月损失”
然后我创建数据框 df。我怎样才能简单地将 df 保存到“每月保费”表中?
我认为这样的东西会起作用,但它会创建一个新的工作表名称。
import pandas as pd
from openpyxl import load_workbook
#sample df
df = pd.DataFrame()
df["A"] = [1,2,3,4,5]
df["B"] = ["A", "B", "C", "D", "E"]
#print(df)
path = r'C:\My\Path\Template1.xlsx'
book = load_workbook(path)
writer = pd.ExcelWriter(path, engine='openpyxl')
writer.book = book
df.to_excel(writer, sheet_name='Monthly Premium')
writer.save()
writer.close()
噜噜哒
相关分类