IWETHEY v. 0.3.0 | TODO
1,095 registered users | 1 active user | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Python and Excel
The calls to the Win32 COM APIs are causing me some grief in a Python script of mine. The script is intended to convert text into numbers (this reason they got that way is another story). The script below works fine with the excption that the Excel process does not really Quit. Instead, Excel is left open as a process, and I have to load up Excel with the file in question and do a manual save.

Anyone know why the Python script doesn't want to close the Excel process?

#!/usr/local/bin/python\nimport sys\nimport win32com.client\n\ndef excelFixNum(filename):\n   xlapp = win32com.client.Dispatch('Excel.Application')\n   xlbook = xlapp.Workbooks.Open(filename)\n\n   for wsi in range(1, xlbook.Worksheets.Count+1):\n      xlsheet = xlbook.Worksheets(wsi)\n      row = 3\n      column = 2\n      while (xlsheet.Cells(row,column).Value is not None):\n         while (xlsheet.Cells(row,column).Value is not None):\n            xlsheet.Cells(row,column).Value = float(xlsheet.Cells(row,column).Value)\n            column = column + 1\n         row = row + 1\n         column = 2\n      del(xlsheet)\n\n   xlbook.Save\n   xlbook.Close\n   xlapp.Quit\n   del(xlbook)\n   del(xlapp)\n\nexcelFixNum("myfile.xls")

New Best guess: there's a dialog box waiting for user input
Check around in the methods for a quieter shutdown operation. I have this issue with Office apps all the time; as Microsoft themselves are fond of saying in several KB articles, Office was never intended for multi-user (i.e. remote, unattended = programmatic) functionality.

Many fears are born of stupidity and ignorance -
Which you should be feeding with rumour and generalisation.
BOfH, 2002 "Episode" 10
New I thought the DisplayAlerts property might be it
But I can't see much difference. IIRC, that property needed to be hit in VB to keep it out of interactive mode. I'm trying

   xlapp.DisplayAlerts = 0


The Save method doesn't seem to have any way to tweak it. I guess I'll try the SaveAs method and see if it does any better.
New Like this one:
[link|http://support.microsoft.com/default.aspx?scid=kb;en-us;Q257757|http://support.micro...=kb;en-us;Q257757]
New Dunno if this applies, but...
when I've encountered XL not shutting down properly, sometimes it is because an implicit object pointer that has been created by the running instance of XL is not set to nothing (the app will only close once all pointers are set to nothing).
bcnu,
Mikem

The soul and substance of what customarily ranks as patriotism is moral cowardice and always has been...We have thrown away the most valuable asset we had-- the individual's right to oppose both flag and country when he (just he, by himself) believed them to be in the wrong. We have thrown it away; and with it all that was really respectable about that grotesque and laughable word, Patriotism.

- Mark Twain, "Monarchical and Republican Patriotism"
New del(obj) should do the same in Python
But it's possible that the Garbage Collector never calls the destructor before it exits the script. Tried calling the GC manually, but it didn't make much difference.
New Sorry.
XL automation is a nightmare in general. Good luck.
bcnu,
Mikem

The soul and substance of what customarily ranks as patriotism is moral cowardice and always has been...We have thrown away the most valuable asset we had-- the individual's right to oppose both flag and country when he (just he, by himself) believed them to be in the wrong. We have thrown it away; and with it all that was really respectable about that grotesque and laughable word, Patriotism.

- Mark Twain, "Monarchical and Republican Patriotism"
New Did you get this fixed? If not,...
you might try setting the "DisplayAlerts" property to false.

I'm having a similar problem with Powerpoint right now. I'm trying to PostMessage WM_CLOSE and it won't work because I have a modal dialog. This doesn't happen w/XL when the DisplayAlerts property is set to true and so far I've been unable to find a way w/Powerpoint to turn the damned Alerts off! (you can't close an MS office app w/a modal dialog displayed - even if the modal dialog is not visible).

hth.
bcnu,
Mikem

The soul and substance of what customarily ranks as patriotism is moral cowardice and always has been...We have thrown away the most valuable asset we had-- the individual's right to oppose both flag and country when he (just he, by himself) believed them to be in the wrong. We have thrown it away; and with it all that was really respectable about that grotesque and laughable word, Patriotism.

- Mark Twain, "Monarchical and Republican Patriotism"
New DisplayAlerts actually made it worse...
...because when it didn't close the files down successfully (same as before), but it also didn't give me a choice when I load up the files manually whether I wanted to keep the changes. For now, I can modify the files and then open them up in Excel. I'm having to go in a manually change a few things that anyhow, so it's no time burner.

Will try to fix it one of these days. Saw [link|http://mail.python.org/pipermail/python-win32/2002-October/000528.html|Someone Else] had a similar problem in Python.
New Ain't MS Office wonderful?
I feel your pain, but I don't think it's a Python problem ;-)
bcnu,
Mikem

The soul and substance of what customarily ranks as patriotism is moral cowardice and always has been...We have thrown away the most valuable asset we had-- the individual's right to oppose both flag and country when he (just he, by himself) believed them to be in the wrong. We have thrown it away; and with it all that was really respectable about that grotesque and laughable word, Patriotism.

- Mark Twain, "Monarchical and Republican Patriotism"
     Python and Excel - (ChrisR) - (9)
         Best guess: there's a dialog box waiting for user input - (tseliot) - (2)
             I thought the DisplayAlerts property might be it - (ChrisR)
             Like this one: - (FuManChu)
         Dunno if this applies, but... - (mmoffitt) - (2)
             del(obj) should do the same in Python - (ChrisR) - (1)
                 Sorry. - (mmoffitt)
         Did you get this fixed? If not,... - (mmoffitt) - (2)
             DisplayAlerts actually made it worse... - (ChrisR) - (1)
                 Ain't MS Office wonderful? - (mmoffitt)

We're gonna make this night... last fo-evah!
113 ms