Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beginner Python: Saving an excel file while it is open

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?

like image 527
Brian HK Avatar asked Nov 02 '22 14:11

Brian HK


2 Answers

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.

like image 71
SpliFF Avatar answered Nov 11 '22 17:11

SpliFF


Hah, funny I ran across your post. I actually just implemented this tonight.

The issue is that Excel files write, and that's it, not both. You cannot read/write off the same object. So if you have another method to save data please do. I'm in a position where I don't have an option.. and so might you.

You're going to need xlutils it's the bread and butter to this.

Here's some example code:

from xlutils.copy import copy

wb_filename = 'example.xls'

wb_object = xlrd.open_workbook(wb_filename)

# And then you can read this file to your hearts galore.

# Now when it comes to writing to this, you need to copy the object and work off that.

write_object = copy(wb_object)

# Write to it all you want and then save that object. 

And that's it, now if you read the object, write to it, and read the original one again it won't be updated. You either need to recreate wb_object or you need to create some sort of table in memory that you can keep track of while working through it.

like image 20
Matthew Avatar answered Nov 11 '22 19:11

Matthew