excel - Marshaling COM objects between Python processes using pythoncom -
i'm hoping can me being able make marshaled cross-process call excel python.
i have excel session initiated via python know , running when needs accessed separate python process. have got working desired using marshaling comarshalinterfaceinstream()
, cogetinterfaceandreleasestream()
calls pythoncom module, need repeat access stream (which can set once in case), , cogetinterfaceandreleasestream()
allows once-only access interface.
i believe achieve can done createstreamonhglobal()
, comarshalinterface()
, counmarshalinterface()
unable working, because not passing in correct parameters.
rather describe in detail main scenario, have set simple example program follows - takes place in same process 1 step @ time! following snippet works fine:
import win32com.client import pythoncom excelapp = win32com.client.dispatchex("excel.application") marshalledexcelapp = pythoncom.comarshalinterthreadinterfaceinstream(pythoncom.iid_idispatch, excelapp) xlapp = win32com.client.dispatch( pythoncom.cogetinterfaceandreleasestream(marshalledexcelapp, pythoncom.iid_idispatch)) xlwb = xlapp.workbooks.add() xlws = xlwb.worksheets.add() xlws.range("a1").value = "aaa"
however, when try following:
import win32com.client import pythoncom excelapp = win32com.client.dispatchex("excel.application") mystream = pythoncom.createstreamonhglobal() pythoncom.comarshalinterface(mystream, pythoncom.iid_idispatch, excelapp, pythoncom.mshctx_local, pythoncom.mshlflags_tablestrong) myunmarshaledinterface = pythoncom.counmarshalinterface(mystream, pythoncom.iid_idispatch)
i error (which imagine related 3rd parameter) when making call pythoncom.comarshalinterface()
:
"valueerror: argument not com object (got type=instance)"
does know how can simple example working?
thanks in advance
after angst, have managed resolve issue facing, , indeed subsequent ones describe.
first, correct in guessing initial problem 3rd parameter in call pythoncom.comarshalinterface(). in fact, should have been making reference oleobj property of excelapp variable:
pythoncom.comarshalinterface(mystream, pythoncom.iid_idispatch, excelapp._oleobj_, pythoncom.mshctx_local, pythoncom.mshlflags_tablestrong)
however, faced different error message, time in call pythoncom.counmarshalinterface():
com_error: (-2147287010, 'a disk error occurred during read operation.', none, none)
it turned out due fact stream pointer needs reset prior use, seek() method:
mystream.seek(0,0)
finally, although aspects working correctly, found despite using quit() on marshalled excel object , explicitly setting variables none prior end of code, left zombie excel process. despite fact pythoncom._getinterfacecount() returning 0.
it turns out had explicitly empty stream had created call coreleasemarshaldata() (having first reset stream pointer again). so, example code snippet in entirety looks this:
import win32com.client import pythoncom pythoncom.coinitialize() excelapp = win32com.client.dispatchex("excel.application") mystream = pythoncom.createstreamonhglobal() pythoncom.comarshalinterface(mystream, pythoncom.iid_idispatch, excelapp._oleobj_, pythoncom.mshctx_local, pythoncom.mshlflags_tablestrong) excelapp = none mystream.seek(0,0) myunmarshaledinterface = pythoncom.counmarshalinterface(mystream, pythoncom.iid_idispatch) unmarshalledexcelapp = win32com.client.dispatch(myunmarshaledinterface) # stuff in excel in order prove marshalling has worked. unmarshalledexcelapp.visible = true xlwbs = unmarshalledexcelapp.workbooks xlwb = xlwbs.add() xlwss = xlwb.worksheets xlws = xlwss.add() xlrange = xlws.range("a1") xlrange.value = "aaa" unmarshalledexcelapp.quit() # clear stream have finished mystream.seek(0,0) pythoncom.coreleasemarshaldata(mystream) xlrange = none xlws = none xlwss = none xlwb = none xlwbs = none myunmarshaledinterface = none unmarshalledexcelapp = none mystream = none pythoncom.couninitialize()
i hope helps else out there overcome obstacles faced!
Comments
Post a Comment