[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

open excel file while it is being used.

barry.zhao

1/10/2008 8:39:00 PM

Hi,

I have a python program that constantly updates an excel spreadsheet.
I would like to be able to view its updates while using excel to edit
other excel files. Below are the test codes I have:

--------------------------------------------------------------------------------------
from time import sleep
import win32com.client as w32c

def test():
w32c.pythoncom.CoInitialize()
print w32c.pythoncom._GetInterfaceCount()
app1 = w32c.Dispatch("Excel.Application")
#app1.Visible=False
#app1.Interactive = False
wb=app1.Workbooks.Open("c:\\temp\\Book1.xls")

sleep(3)
sh=wb.Sheets("Sheet1")
sh.Cells(2,1).Value = "Hello, world!"
sh = None

wb.Save()
wb = None
app1 = None
w32c.pythoncom.CoUninitialize()
print w32c.pythoncom._GetInterfaceCount()
-------------------------------------------------------------------------------------

If the user just looks at the file then it's fine, but if the user
double clicks one cell, which will have a cursor flashing in the cell,
then it will lead to a crash of the program with error: "Call was
rejected by callee." I can make the program keep trying to access the
file, but if the user doesn't make the flashing cursor disappear, the
program will wait forever!

I know excel has the fuction such that when a user is accessing a file
and other users open that file, it will prompt for options of opening
it as read-only or sending notification. I have been trying to
implement that in the program, but don't know how.

So, any suggestion will be greatly appreciated!

- Barry
1 Answer

Devraj

1/11/2008 2:45:00 AM

0

Hi Barry,

Obviously what you are trying to do is detect file locks. I am not
exactly sure how to do this in Python but from previous system
administration jobs here are some scenarios that you might want to
watch out for.

- I know for one that if you are accessing the file on a Samba share,
file locking with OpenOffice or Excel have strange things happen to
them

- I am also aware that the same thing works different if the files are
shared from a Windows server and accessed on a Linux machine, and
depends on the version of Samba/Linux kernel you are running


On Jan 11, 7:38 am, barry.z...@gmail.com wrote:
> Hi,
>
> I have a python program that constantly updates an excel spreadsheet.
> I would like to be able to view its updates while using excel to edit
> other excel files. Below are the test codes I have:
>
> --------------------------------------------------------------------------------------
> from time import sleep
> import win32com.client as w32c
>
> def test():
> w32c.pythoncom.CoInitialize()
> print w32c.pythoncom._GetInterfaceCount()
> app1 = w32c.Dispatch("Excel.Application")
> #app1.Visible=False
> #app1.Interactive = False
> wb=app1.Workbooks.Open("c:\\temp\\Book1.xls")
>
> sleep(3)
> sh=wb.Sheets("Sheet1")
> sh.Cells(2,1).Value = "Hello, world!"
> sh = None
>
> wb.Save()
> wb = None
> app1 = None
> w32c.pythoncom.CoUninitialize()
> print w32c.pythoncom._GetInterfaceCount()
> -------------------------------------------------------------------------------------
>
> If the user just looks at the file then it's fine, but if the user
> double clicks one cell, which will have a cursor flashing in the cell,
> then it will lead to a crash of the program with error: "Call was
> rejected by callee." I can make the program keep trying to access the
> file, but if the user doesn't make the flashing cursor disappear, the
> program will wait forever!
>
> I know excel has the fuction such that when a user is accessing a file
> and other users open that file, it will prompt for options of opening
> it as read-only or sending notification. I have been trying to
> implement that in the program, but don't know how.
>
> So, any suggestion will be greatly appreciated!
>
> - Barry