我尝试多次运行此程序,但现有的excel文件未更新。有人可以指导我完成正确的过程吗?感谢您的帮助!import pandas as pdimport openpyxl as plmanifest_df = pd.read_excel(r'C:\Users\dhruvjadhav\PycharmProjects\Alpha\PassengerManifest.xlsx')manifest_df['Current Balance'] = manifest_df['Balance'] - manifest_df['Fare']print(manifest_df)filename = (r'C:\Users\dhruvjadhav\PycharmProjects\Alpha\PassengerManifest.xlsx')wb = pl.load_workbook(filename)ws = wb.worksheets[0]for ws in wb.worksheets: print(ws.title)with pd.ExcelWriter("PassengerManifest.xlsx", engine="openpyxl", mode="a") as writer: manifest_df.to_excel(writer, sheet_name="ws.title")writer.save()
1 回答
狐的传说
TA贡献1804条经验 获得超3个赞
如果要更新现有 Excel,可以使用以下代码。只需要熊猫。
import pandas as pd
# read the xlsx file
manifest_df = pd.read_excel(r'C:\Users\dhruvjadhav\PycharmProjects\Alpha\PassengerManifest.xlsx')
# perform arithmentic operation
manifest_df['Current Balance'] = manifest_df['Balance'] - manifest_df['Fare']
# write again the excel file
manifest_df.to_excel(r'C:\Users\dhruvjadhav\PycharmProjects\Alpha\PassengerManifest.xlsx', index=False)
就是这样
添加回答
举报
0/150
提交
取消