通过 Python 更新现有的 Excel 文件

我尝试多次运行此程序,但现有的excel文件未更新。有人可以指导我完成正确的过程吗?感谢您的帮助!


import pandas as pd

import openpyxl as pl

manifest_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()


慕妹3242003
浏览 184回答 1
1回答

狐的传说

如果要更新现有 Excel,可以使用以下代码。只需要熊猫。import pandas as pd# read the xlsx filemanifest_df = pd.read_excel(r'C:\Users\dhruvjadhav\PycharmProjects\Alpha\PassengerManifest.xlsx')# perform arithmentic operationmanifest_df['Current Balance'] = manifest_df['Balance'] - manifest_df['Fare']# write again the excel filemanifest_df.to_excel(r'C:\Users\dhruvjadhav\PycharmProjects\Alpha\PassengerManifest.xlsx', index=False)就是这样
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python