1

I use subprocess to open an excel file but I cannot kill it.

import subprocess
p = subprocess.Popen([r"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE", 'data.xlsx'])

p.wait()
p.kill()

The excel file is still present. I searched p.pid in task manager but I cannot find the process. What should I do?

Frankie
  • 744
  • 1
  • 9
  • 14
  • 1
    A *file* is not a *process.* I'm imagining Windows runs something like `start excel` internally and then `p` is finished, even though Excel is running in a separate process elsewhere. You will need to use Windows facilities to find and terminate Excel (but I would in fact advise strongly against killing Excel without the user's consent! What if they originally opened it for something else and have unsaved changes?) – tripleee Feb 09 '18 at 08:14
  • There are issues with killing MS Office processes, in particular Excel. This should be of interest, its not a duplicate, but close: https://stackoverflow.com/questions/17781708/why-is-killing-the-excel-process-a-bad-thing also this: https://stackoverflow.com/questions/18421457/com-excelapplication-application-quit-preserves-the-process/18421814#18421814 – cdarke Feb 09 '18 at 08:26

1 Answers1

1

After doing some research in the standard python library and searching around I found this solution:

from subprocess import Popen

p = Popen([r"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE", 'data.xlsx'], shell=True)
if p.poll() is None:
    Popen("taskkill /F /IM EXCEL.EXE",shell=True)

I found the second part here: Close Excel using CMD

I used shell=true in order to have the command completed in a cmd shell window. p.poll() basically asks whether or not the program has been killed and it also gives the excel file time to open.

After working this for a bit, I am answering your question but based on my experience, unless command line is required, I always try to avoid it and use either a standard python library or a sturdy external library as subprocess can get pretty complicated with timing and input/outputs. If the excel file takes awhile to load you may run into an issue where it is trying to close the file before it exists. So here you may want to run a timer.

Also forgot to mention that killing the tasks this way will lose any unsaved data in the file, like i said timing will seem to be an issue.

  • Thank you, Jonathan. Your suggested solution can kill the excel file but in fact it will kill ALL the excel files including the ones which I don't want to close. Any other methods to close just 'data.xlsx'? – Frankie Feb 09 '18 at 10:04
  • Hey Frankie. No unfortunately i did some research and found no easy way to close the one file. I did play around with the idea of closing a file by name command in CMD, but I dont have any further information on it. Closing the file after it opens, im not sure how you are using this file but i would suggest this. Use another way to open the file and close it using python or VBA or batch file. if you want to continue using python to open, write a macro that would close it after its done with whatever youre doing. – Jonathan de la Rosa Feb 11 '18 at 10:24