简体   繁体   English

Pandas / xlsxwriter writer.close() 没有完全关闭 excel 文件

[英]Pandas / xlsxwriter writer.close() does not completely close the excel file

I'm trying to modify manually an excel file after creating it with a python script.在使用 python 脚本创建一个 excel 文件后,我试图手动修改它。 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()

I expect python to fully close the excel file and let me overwrite on it while the script is still running我希望 python 完全关闭 excel 文件并让我在脚本仍在运行时覆盖它

I also had this issue.我也有这个问题。 When trying to save changes in Excel I got "Sharing violation".尝试在 Excel 中保存更改时,出现“共享冲突”。 Solved it adding writer.handles = None after writer.close() .解决了在writer.close()之后添加writer.handles = None

writer = pd.ExcelWriter(workPath+'file.xlsx', engine='xlsxwriter')
# Add all your sheets and formatting here
...
# Save and release handle
writer.close()
writer.handles = None

Your code looks too complicated, you don't need to deal with the writer yourself df.to_excel() can do it for you.您的代码看起来太复杂了,您不需要自己与df.to_excel()可以为您完成。 Just use the simpler code: df1.to_excel(outpath, sheet_name="Results", engine='xlsxwriter') as suggested in the docs .只需使用更简单的代码: df1.to_excel(outpath, sheet_name="Results", engine='xlsxwriter')按照文档中的建议。

I also ran into this.我也遇到了这个。 I couldn't save the file in Excel because of a "Sharing violation" because python.exe still had a handle on the file.由于“共享冲突”,我无法将文件保存在 Excel 中,因为 python.exe 仍然拥有该文件的句柄。

The accepted answer, to just use df.to_excel() is correct if all you want to do is save the excel file.接受的答案,如果您只想保存 excel 文件,只使用df.to_excel()是正确的。 But if you want to do more things, such as adding formatting to the excel file first, you will have to use pd.ExcelWriter() .但是如果你想做更多的事情,比如pd.ExcelWriter() excel 文件添加格式,你就必须使用pd.ExcelWriter()

The key is though, as Exho commented, that you use the form:不过,正如 Exho 评论的那样,关键是您使用了以下表格:

with pd.ExcelWriter(outpath, engine="xlsxwriter") as writer:
    # do stuff here

You don't use writer.save() or writer.close() , which are synonyms for the same call anyway.您不使用writer.save()writer.close() ,它们无论如何都是同一个调用的同义词。 Instead the file is saved and closed and handles are released as soon as you leave the with scope.相反,一旦离开with作用域,文件就会被保存并关闭,并且句柄会被释放。

I was facing a similar situation.我面临着类似的情况。 The suggestion given by alec_djinn didn't work for multiple sheets, as I was working. alec_djinn 给出的建议不适用于多张纸,因为我正在工作。 So I just ignored .close() method and it worked just fine.所以我只是忽略了 .close() 方法,它工作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在我的 Pandas 和 Openpyxl 代码中使用 Writer.Close() function 时出错 - Error when using Writer.Close() function within my Pandas and Openpyxl code 即使在 writer.close() 运行后,Asyncio Streams 也会自动重新启动以读取数据 - Asyncio Streams automatically restarts to read data even after writer.close() is run xlsxwriter模块无法正确打开/关闭Excel文件 - xlsxwriter module won't open/close Excel file correctly Xlsxwriter在Django项目中找不到close()的文件 - Xlsxwriter cant find file for close() in Django Project 使用xlsxwriter关闭然后使用xlrd读取excel文件后,无法读取公式的值 - Fails to read the value of formular after using xlsxwriter to close and then usinging xlrd to read the excel file 无法在 xlsxwriter 中关闭工作表 - Unable to close worksheet in xlsxwriter 使用XlsxWriter将pandas图表插入Excel文件 - Insert pandas chart into an Excel file using XlsxWriter 使用xlsxwriter 将pandas df 写入excel 文件? - Write pandas df into excel file with xlsxwriter? 将内容写入excel csv文件时,csv.writer不会关闭 - csv.writer won't close when writing things into an excel csv file 使用 xlsxwriter 将文本文件数据迁移到 excel 未完全更新 - Migrating text file data to excel using xlsxwriter is not updating completely
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM