如何在 openpyxl 进行更改时观察 Excel 文件更改 [英] How to Watch Excel File Change While openpyxl Makes Changes

查看:35
本文介绍了如何在 openpyxl 进行更改时观察 Excel 文件更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个 iOS 应用,它接受用户的输入,然后将他们的数据作为 .csv 文件上传到 Dropbox.

I built an iOS app that takes users' inputs then uploads their data as .csv files onto dropbox.

现在,我想构建一个跨平台的桌面应用程序,它使用 openpyxl 和 Python dropbox api 来侦听这些 .csv 上传,然后将这些 .csv 文件中的数据写入各种 .xlsx 工作簿.我对此没有任何问题,除了我的客户希望能够在 excel 工作簿更改时在他们的桌面上观看.

Now, I'd like to build a cross-platform desktop app that uses openpyxl and the Python dropbox api to listen for these .csv uploads and then write the data in these .csv files to various .xlsx workbooks. I have no issues with this, except my client wants to be able to watch on their desktop as the excel workbooks change.

每当我使用 openpyxl 进行赋值时,例如...

Whenever I make an assignment with openpyxl, such as...

wb = load_workbook("sample.xlsx") #load an existing .xlsx file
ws = wb.active #get active worksheet
ws['A1'] = 12 #assign data to cell A1

...然后运行此脚本将成功更改文件,但我必须关闭然后打开文件才能看到更改,而不是看着更改发生.

... then running this script will successfully alter the file, BUT I have to close and then open the file to see the change, instead of watching the change happen as it happens.

所以,我的问题是,我如何观察 openpyxl 对 Excel 工作簿的更改发生时发生的变化,是否有跨平台解决方案?解决问题的替代方法表示赞赏!

So, my question is, how do I watch openpyxl's changes to excel workbooks happen as they occur, and is there a cross-platform solution for this? Alternate ways of approaching the problem are appreciated!

我对此的研究持续了几个小时,我想出的最好的是 AppleScript 和 pywin32 的一些混合体,但这可能会很快变得非常丑陋/错误!

My research on this has spanned a few hours and the best I have come up with is some hybrid of AppleScript and pywin32, but that could get real ugly/buggy real fast!

非常感谢您的帮助!

推荐答案

我不认为您可以轻松做到这一点.Excel 文件本身只是 zip 存档,这使得监控变得非常困难,因为您无法就地修改 zip 文件:您总是创建一个新存档.这正是 Excel 采用文件锁定的原因.

I don't think you can do this easily. Excel files are themselves simply zip archives and this makes monitoring very difficult because you cannot modify zip files in place: you always create a new archive. This is exactly why Excel employs file-locking.

在我看来,您需要某种 Excel 本身可以监控和集成的外部数据源.您可能想查看 xlwings 进行就地编辑.或者,也可以使用 openpyxl 创建外部数据源.它适用于外部文件.

It sounds to me like you'd need some kind of external data source that Excel itself can monitor and integrate. You might want to look at xlwings for in place editing. Alternatively, it might be possible to use openpyxl to create an external data source. It will work with external files.

这篇关于如何在 openpyxl 进行更改时观察 Excel 文件更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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