为了账号安全,请及时绑定邮箱和手机立即绑定

通过 Python 更新现有的 Excel 文件

通过 Python 更新现有的 Excel 文件

慕妹3242003 2022-09-06 18:11:13
我尝试多次运行此程序,但现有的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)

就是这样


查看完整回答
反对 回复 2022-09-06
  • 1 回答
  • 0 关注
  • 183 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信