Hello,
From my python script I can read/write Excel files without a problem
However, I can not completely “kill” Excel process from the script once I’m done with it…
There is an old thread on the forum about it but with no final solution.
Actually there are some guidelines which I do not understand…
( in the last post of the thread are mentioned “two dots” )
[Close an Excel session with Python]
I did not want to resurrect this old thread but I still need a solution
BR
Aleksandar
1 Like
In c# it would be something like
var excel=Process.GetProcessesByName("Excel")
excel.Kill();
Which you can easily translate in to python. Just make sure the process name is correct, otherwise you will get an exception. Also make sure you have a reference to the System.Diagnostics
namespace.
Hi Aleksandar,
It’s been a while! I do not use Rhino from 2014! I Think the two dots rule is explained in the screenshot linked. I do not remember the problem, i’m sorry. Try to create excel’s objects once at the time and use temporary variable. At the end Call dispose() method on each to let garbage collector work.
I hope i’m helpfull.
Inviato dal mio BlackBerry, il dispositivo mobile più sicuro
Da: mcneel@discoursemail.com
Inviato: 14 febbraio 2021 21:03
A: daniele@jsmservice.eu
Rispondi a: mcneel+0dce607e97ac86e9e6d55cd71abaf9b2@discoursemail.com
Oggetto: [McNeel Forum] [Scripting] End MS Excel process with python script
|
| AleksandarSM
February 14 |
Hello,
However, I can not completely “kill” Excel process from the script once I’m done with it…
There is an old thread on the forum about it but with no final solution.
Actually there are some guidelines which I do not understand…
( in the last post of the thread are mentioned “two dots” )
[Close an Excel session with Python]
I did not want to resurrect this old thread but I still need a solution
BR
Aleksandar
Thanks for the quick answer.
It works but it kills all excel instances on my pc, not only the one I used in the script…
Is there a way to identify “my” excel process? - to kill only that one
if someone needs it, here is the translation:
I had to modify the script little bit because ‘excel’ variable is an array of processes
( that is why it kills them all )
excel = System.Diagnostics.Process.GetProcessesByName("Excel")
for proc in excel:
proc.Kill()
for the reference, I created Excel instance with this python code:
import clr
clr.AddReference("Microsoft.Office.Interop.Excel")
from Microsoft.Office.Interop import Excel
xl=Excel.ApplicationClass()
WB=xl.Workbooks.Open (FileName)
SH1=WB.Worksheets("Sheet1")
...
Hello DanieleMauro,
thanks for the reply - I modified the script but still can not make it…
maybe it is because I do not know how to perform ReleaseComObject() in python?
here is the core python code:
import rhinoscriptsyntax as rs
import clr
clr.AddReference("Microsoft.Office.Interop.Excel")
from Microsoft.Office.Interop import Excel
def Excel_Open():
myfile = rs.OpenFileName("Select Excel File:","Excel file|*.xls;*.xlsx|")
if not myfile:
return
xl = Excel.ApplicationClass()
WBS= xl.Workbooks
WB=WBS.Open (myfile)
print WB.Name
WB.Close()
xl.Quit()
WBS=None
WB=None
xl=None
Excel_Open()
I see what you mean, try something like this
System.Runtime.InteropServices.Marshal.ReleaseComObject(an excel object); // typical way to dispose a COM object.
// do more stuff, saving etc
yourExcelAppInstance.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(yourExcelAppInstance);
Sorry for any weird formatting in my reply, I am using my phone. Hope this helps though.
After giving up the issue for a while, I think I came up with a (so far) working solution
I have posted solution in the other thread
https://discourse.mcneel.com/t/close-an-excel-session-with-python/613/12?u=aleksandarsm
BR