初学者Python:打开Excel文件时保存Excel文件 [英] Beginner Python: Saving an excel file while it is open

查看:840
本文介绍了初学者Python:打开Excel文件时保存Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题,我希望有一个简单的解决方案。



我正在使用xlwt包编写excel文件来编写python(2.7)代码。程序将数据写入并保存在不断保存的文件中。问题是,每当我打开文件来检查数据,并且python尝试保存文件程序崩溃。



当我打开阅读时,有没有办法让python保存文件?

解决方案

我的经验是,sashkello是正确的,Excel锁定文件。即使是OpenOffice / LibreOffice也是这样做的。他们将文件锁定在磁盘上,并创建一个临时版本作为工作副本。任何试图访问打开文件的程序将被操作系统拒绝。原因是因为许多公司将Excel文件视为数据库,但用户对并发和同步中涉及的问题没有了解。



我在linux上,我得到这种行为(至少当文件在SAMBA共享上时)。查看与您的文件相同的目录,如果一个名为。〜lock。[filename]#的文件存在,那么您将无法从另一个程序读取您的文件。我不知道什么是强制这个锁,但我怀疑这是一个NTFS属性。请注意,即使一个简单的 cp cat 失败: cp:错误阅读'CATALOGUE.ods ':输入/输出错误



更新:实际的锁定机制似乎是oplocks,这是一个连接到Windows共享的概念: http://oreilly.com/openbook/samba/book/ch05_05.html 。如果共享由Samba管理,解决方法是禁用特定文件类型的锁,例如:



否决oplock文件= /*.xlsx/



如果您没有在linux上使用共享或NTFS,那么我想你应该能够RW文件只要你的脚本有写权限默认情况下只有创建该文件的用户具有写访问权限。



替代方法2:只有在Excel / LO中打开该文件作为可写入时,但是,LO至少允许您以只读方式打开文件(转到文件 - >属性 - >安全性,设置只读,保存并重新打开文件)。我不知道这是否也会使它成为xlwt的RO。


I have a simple problem that I hope will have a simple solution.

I am writing python(2.7) code using the xlwt package to write excel files. The program takes data and writes it out to a file that is being saved constantly. The problem is that whenever I have the file open to check the data and python tries to save the file the program crashes.

Is there any way to make python save the file when I have it open for reading?

解决方案

My experience is that sashkello is correct, Excel locks the file. Even OpenOffice/LibreOffice do this. They lock the file on disk and create a temp version as a working copy. ANY program trying to access the open file will be denied by the OS. The reason for this is because many corporations treat Excel files as databases but the users have no understanding of the issues involved in concurrency and synchronisation.

I am on linux and I get this behaviour (at least when the file is on a SAMBA share). Look in the same directory as your file, if a file called .~lock.[filename]# exists then you will be unable to read your file from another program. I'm not sure what enforces this lock but I suspect it's an NTFS attribute. Note that even a simple cp or cat fails: cp: error reading ‘CATALOGUE.ods’: Input/output error

UPDATE: The actual locking mechanism appears to be 'oplocks`, a concept connected to Windows shares: http://oreilly.com/openbook/samba/book/ch05_05.html . If the share is managed by Samba the workaround is to disable locks on certain file types, eg:

veto oplock files = /*.xlsx/

If you aren't using a share or NTFS on linux then I guess you should be able to RW the file as long as your script has write permissions. By default only the user who created the file has write access.

WORKAROUND 2: The restriction only seems to apply if you have the file open in Excel/LO as writable, however LO at least allows you to open a file as read-only (Go to File -> Properties -> Security, set Read-Only, Save and re-open the file). I don't know if this will also make it RO for xlwt though.

这篇关于初学者Python:打开Excel文件时保存Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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