Saturday, 24 August 2013

COM: excelApplication.Application.Quit() preserves the process

COM: excelApplication.Application.Quit() preserves the process

I'm using COM integration to drive MS Excel from Python 2.7. I noticed a
peculiar thing: when I run the following bit of code:
import win32com.client
excelApp = win32com.client.dynamic.Dispatch('Excel.Application')
an EXCEL.EXE process appears on the processes list (which view using the
Windows Task Manager or subprocess.Popen('tasklist')) as expected. I then
do all the stuff I need to do no problem. However, when I close Excel:
excelApp.Application.Quit()
The process persists, even if I close the Python interpreter which started
it (this kind of makes sense as Excel runs in a different process but just
to be sure). The only way I've found to terminate this process is either
by hand, using the Task Manager, or by calling:
subprocess.Popen("taskkill /F /im EXCEL.EXE",shell=True)
the forceful /F flag is necessary, otherwise the process doesn't terminate.
This isn't really a problem (I hope) but I wanted to ask whether this
could cause issues when I first edit the documents "normally", then when
calling Excel from python and then "normally" again? Potentially many
(couple dozens) times in a row? What I'm worried about is creating
conflicting versions of documents etc. Or should I just terminate the
EXCEL.EXE process each time just to be safe?
Also I noticed that subprocess.Popen("taskkill") doesn't return any
exceptions that I can catch and anylse (or am I doing something worng
here?). I'm particularly interested in distinguishing between the
"non-existent process" kill attempt and a failed attempt to terminate the
process.

No comments:

Post a Comment