How to correctly handle events using win32com ?

Gauthier ABRIAL <[email protected]> Fri, 29 Nov 2024 17:17:12 +0000
Newsgroups gmane.comp.python.windows
Message-ID <SJ2PR16MB5986E919FA3D3E0BC4F6BB19E22A2@SJ2PR16MB5986.namprd16.prod.outlook.com>
--===============4251422037672012262==
Content-Language: fr-CA
Content-Type: multipart/alternative;
	boundary="_000_SJ2PR16MB5986E919FA3D3E0BC4F6BB19E22A2SJ2PR16MB5986namp_"

--_000_SJ2PR16MB5986E919FA3D3E0BC4F6BB19E22A2SJ2PR16MB5986namp_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hello,
I'm looking for some advice on how to correctly handle events using win32co=
m. If I take the example of a very simple Excel automation, I tested two th=
ings.

  *
Code1 below use pythoncom.PumpMessages() but I don't know how to stop it wh=
en the Excel is closing. I guess I should send a WM_QUIT message but I don'=
t know how.
  *
Code2 below use a while loop on pythoncom.PumpWaitingMessages() but once I =
stop the loop the COM server freeze as it is waiting for the messages it se=
nds to be processed before closing. I guess I should pump all the remaining=
 messages but I don't know how.

Maybe I should use a totally different approach.
Thanks a lot for your help.

G.

-------------------- Code1 Starts -----------------------
import win32com.client as win32
import pythoncom

#The event handlers
class wbEvents:
    def OnBeforeClose(self, Cancel):
        print('Closing Workbook')
        # Send WM_QUIT here ?

xlApp =3D win32.Dispatch("Excel.Application") #Open Excel
xlApp.Visible=3DTrue #Make Excel Visible
xlwb =3D xlApp.Workbooks.Add() #Create a workbook
ws=3Dxlwb.Sheets("Sheet1") #Get the first worksheet
xl_events=3Dwin32.WithEvents(xlwb,wbEvents) #Add and Event handler
pythoncom.PumpMessages()
-------------------- Code1 Ends -----------------------

-------------------- Code2 Starts -----------------------
import win32com.client as win32
import pythoncom
import time

#The event handlers
class wbEvents:
    def OnBeforeClose(self, Cancel):
        print('Closing Workbook')
        global keepOpen
        keepOpen =3D False

xlApp =3D win32.Dispatch("Excel.Application") #Open Excel
xlApp.Visible=3DTrue #Make Excel Visible
xlwb =3D xlApp.Workbooks.Add() #Create a workbook
ws=3Dxlwb.Sheets("Sheet1") #Get the first worksheet
xl_events=3Dwin32.WithEvents(xlwb,wbEvents) #Add and Event handler
# define initalizer
keepOpen =3D True
while keepOpen:
    time.sleep(0.1)
    pythoncom.PumpWaitingMessages()
-------------------- Code2 Ends -----------------------


--_000_SJ2PR16MB5986E919FA3D3E0BC4F6BB19E22A2SJ2PR16MB5986namp_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style type=3D"text/css" style=3D"display:none;"> P {margin-top:0;margin-bo=
ttom:0;} </style>
</head>
<body dir=3D"ltr">
<div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, san=
s-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hello,</div>
<div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, san=
s-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I'm looking for some advice on how to correctly handle events using win32co=
m. If I take the example of a very simple Excel automation, I tested two th=
ings.</div>
<ul data-editing-info=3D"{&quot;applyListStyleFromLevel&quot;:false,&quot;u=
norderedStyleType&quot;:2}">
<li style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; =
color: rgb(0, 0, 0); list-style-type: &quot;- &quot;;">
<div class=3D"elementToProof">Code1 below use pythoncom.PumpMessages() but =
I don't know how to stop it when the Excel is closing. I guess I should sen=
d a WM_QUIT message but I don't know how.</div>
</li><li style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 1=
2pt; color: rgb(0, 0, 0); list-style-type: &quot;- &quot;;">
<div class=3D"elementToProof" style=3D"margin: 0px;">Code2 below use a whil=
e loop on pythoncom.PumpWaitingMessages() but once I stop the loop the COM =
server freeze as it is waiting for the messages it sends to be processed be=
fore closing. I guess I should pump
 all the remaining messages but I don't know how.</div>
</li></ul>
<div class=3D"elementToProof" style=3D"margin: 0px; font-family: Calibri, H=
elvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Maybe I should use a totally different approach.</div>
<div class=3D"elementToProof" style=3D"margin: 0px; font-family: Calibri, H=
elvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Thanks a lot for your help.</div>
<div class=3D"elementToProof" style=3D"margin: 0px; font-family: Calibri, H=
elvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class=3D"elementToProof" style=3D"margin: 0px; font-family: Calibri, H=
elvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
G.</div>
<div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, san=
s-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, san=
s-serif; font-size: 12pt; color: rgb(0, 0, 0);">
-------------------- Code1 Starts -----------------------</div>
<div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, san=
s-serif; font-size: 12pt; color: rgb(0, 0, 0);">
import win32com.client as win32</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
import pythoncom</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
<br>
</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
#The event handlers</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
class wbEvents:</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; def OnBeforeClose(self, Cancel):</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; &nbsp; &nbsp; print('Closing Workbook')</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; &nbsp; &nbsp; # Send WM_QUIT here ?</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
<br>
</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
xlApp =3D win32.Dispatch(&quot;Excel.Application&quot;) #Open Excel</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
xlApp.Visible=3DTrue #Make Excel Visible</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
xlwb =3D xlApp.Workbooks.Add() #Create a workbook</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
ws=3Dxlwb.Sheets(&quot;Sheet1&quot;) #Get the first worksheet</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
xl_events=3Dwin32.WithEvents(xlwb,wbEvents) #Add and Event handler</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
pythoncom.PumpMessages()</div>
<div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, san=
s-serif; font-size: 12pt; color: rgb(0, 0, 0);">
-------------------- Code1 Ends -----------------------</div>
<div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, san=
s-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class=3D"elementToProof" style=3D"text-align: left; text-indent: 0px; =
margin: 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; =
color: rgb(0, 0, 0);">
-------------------- Code2 Starts -----------------------</div>
<div style=3D"text-align: left; text-indent: 0px; margin: 0px; font-family:=
 Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
import win32com.client as win32</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
import pythoncom</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
import time</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
<br>
</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
#The event handlers</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
class wbEvents:</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; def OnBeforeClose(self, Cancel):</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; &nbsp; &nbsp; print('Closing Workbook')</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; &nbsp; &nbsp; global keepOpen</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; &nbsp; &nbsp; keepOpen =3D False</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
<br>
</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
xlApp =3D win32.Dispatch(&quot;Excel.Application&quot;) #Open Excel</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
xlApp.Visible=3DTrue #Make Excel Visible</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
xlwb =3D xlApp.Workbooks.Add() #Create a workbook</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
ws=3Dxlwb.Sheets(&quot;Sheet1&quot;) #Get the first worksheet</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
xl_events=3Dwin32.WithEvents(xlwb,wbEvents) #Add and Event handler</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
# define initalizer</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
keepOpen =3D True</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
while keepOpen:</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; time.sleep(0.1)</div>
<div style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt;=
 color: rgb(0, 0, 0);">
&nbsp; &nbsp; pythoncom.PumpWaitingMessages()</div>
<div class=3D"elementToProof" style=3D"text-align: left; text-indent: 0px; =
margin: 0px; font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; =
color: rgb(0, 0, 0);">
-------------------- Code2 Ends -----------------------</div>
<div class=3D"elementToProof" style=3D"font-family: Calibri, Helvetica, san=
s-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
</body>
</html>

--_000_SJ2PR16MB5986E919FA3D3E0BC4F6BB19E22A2SJ2PR16MB5986namp_--

--===============4251422037672012262==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
python-win32 mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-win32

--===============4251422037672012262==--