pandas /xlsxwriter writer.close()不能完全关闭excel文件 [英] Pandas / xlsxwriter writer.close() does not completely close the excel file

查看:58
本文介绍了 pandas /xlsxwriter writer.close()不能完全关闭excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python脚本创建Excel文件后手动对其进行修改.不幸的是,如果脚本仍在运行,则尝试使用相同的名称保存该脚本时会出现共享文件冲突错误消息.

I'm trying to modify manually an excel file after creating it with a python script. Unfortunately, if the script is still running, a sharing file violation error message appears when trying to save it with the same name.

一切都在代码中顺利运行.文件已创建,填充和保存.我可以打开它并对其进行处理,但是如果脚本仍在运行,则不能用相同的名称覆盖它.

Everything runs smoothly in the code. The file is created, filled and saved. I can open it and work on it but can't overwrite it under the same name if the script is still running.

outpath = filedialog.asksaveasfile(
    mode="wb",
    filetype=[("Excel", ("*.xls", "*.xlsx"))],
    defaultextension=".xlsx",
)
writer = pd.ExcelWriter(outpath, engine="xlsxwriter")
df1.to_excel(writer, sheet_name="Results")
writer.save()
writer.close()

我希望python完全关闭excel文件,并让我在脚本仍在运行时对其进行覆盖

I expect python to fully close the excel file and let me overwrite on it while the script is still running

推荐答案

您的代码看起来太复杂了,您无需自己与作者打交道 df.to_excel()可以做到这一点你.只需使用

Your code looks too complicated, you don't need to deal with the writer yourself df.to_excel() can do it for you. Just use the simpler code:df1.to_excel(outpath, sheet_name="Results", engine='xlsxwriter') as suggested in the docs.

这篇关于 pandas /xlsxwriter writer.close()不能完全关闭excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆