Re: How to correctly handle events using win32com ?

dornech via python-win32 <[email protected]> Fri, 29 Nov 2024 20:48:35 +0100
Newsgroups gmane.comp.python.windows
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--===============3872627172263481679==
Content-Type: multipart/alternative;
 boundary="------------nFy5IsTXUwL240lm2vVeQ77y"
Content-Language: de-DE

This is a multi-part message in MIME format.
--------------nFy5IsTXUwL240lm2vVeQ77y
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable

Hi there,

exactly, Mark is right on this.
You can use very COM object method as you use out of VBA for example.

I am working on a wrapper for a more "pythonized" access to the EXCEL
API, you may have alokk on it as soon as it is published.

BG
Chris



Am 29.11.2024 um 20:10 schrieb Mark Hammond:
>
> It's been a while since I've done any of this, but I think there's an
> "xlApp.Quit()" method you can use? If so, then you might be able to
> break out of the look when "PumpWaitingMessages()" returns 1 - that
> will mean the message queue has received a WM_QUIT.
>
>
> On 2024-11-29 12:17 p.m., Gauthier ABRIAL wrote:
>> Hello,
>> I'm looking for some advice on how to correctly handle events using
>> win32com. If I take the example of a very simple Excel automation, I
>> tested two things.
>>
>>  *
>>     Code1 below use pythoncom.PumpMessages() but I don't know how to
>>     stop it when 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 sends 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:
>> =C2=A0 =C2=A0 def OnBeforeClose(self, Cancel):
>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 print('Closing Workbook')
>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 # 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:
>> =C2=A0 =C2=A0 def OnBeforeClose(self, Cancel):
>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 print('Closing Workbook')
>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 global keepOpen
>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 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:
>> =C2=A0 =C2=A0 time.sleep(0.1)
>> =C2=A0 =C2=A0 pythoncom.PumpWaitingMessages()
>> -------------------- Code2 Ends -----------------------
>>
>>
>> _______________________________________________
>> python-win32 mailing list
>> [email protected]
>> https://mail.python.org/mailman/listinfo/python-win32
>
> _______________________________________________
> python-win32 mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/python-win32

--------------nFy5IsTXUwL240lm2vVeQ77y
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3DUTF-=
8">
  </head>
  <body>
    Hi there,<br>
    <br>
    exactly, Mark is right on this.<br>
    You can use very COM object method as you use out of VBA for
    example.<br>
    <br>
    I am working on a wrapper for a more "pythonized" access to the
    EXCEL API, you may have alokk on it as soon as it is published.<br>
    <br>
    BG <br>
    Chris<br>
    <br>
    <br>
    <br>
    <div class=3D"moz-cite-prefix">Am 29.11.2024 um 20:10 schrieb Mark
      Hammond:<br>
    </div>
    <blockquote type=3D"cite"
      cite=3D"mid:[email protected]">
      <meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3DUT=
F-8">
      <p><tt>It's been a while since I've done any of this, but I think
          there's an "xlApp.Quit()" method you can use? If so, then you
          might be able to break out of the look when
          "PumpWaitingMessages()" returns 1 - that will mean the message
          queue has received a WM_QUIT.</tt></p>
      <p><tt><br>
        </tt></p>
      <div class=3D"moz-cite-prefix">On 2024-11-29 12:17 p.m., Gauthier
        ABRIAL wrote:<br>
      </div>
      <blockquote type=3D"cite"
cite=3D"mid:[email protected].=
prod.outlook.com">
        <meta http-equiv=3D"Content-Type"
          content=3D"text/html; charset=3DUTF-8">
        <style type=3D"text/css" style=3D"display:none;">P {margin-top:0;m=
argin-bottom:0;}</style>
        <div class=3D"elementToProof"
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          Hello,</div>
        <div class=3D"elementToProof"
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          I'm looking for some advice on how to correctly handle events
          using win32com. If I take the example of a very simple Excel
          automation, I tested two things.</div>
        <ul
data-editing-info=3D"{&quot;applyListStyleFromLevel&quot;:false,&quot;unor=
deredStyleType&quot;:2}">
          <li
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: 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 send a WM_QUIT
              message but I don't know how.</div>
          </li>
          <li
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0); list-style-type: &quot;- &quot;;">
            <div class=3D"elementToProof" style=3D"margin: 0px;">Code2 bel=
ow
              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 sends to be processed before
              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, Helvetica, sans-serif; font-si=
ze: 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, Helvetica, sans-serif; font-si=
ze: 12pt; color: rgb(0, 0, 0);">
          Thanks a lot for your help.</div>
        <div class=3D"elementToProof"
style=3D"margin: 0px; font-family: Calibri, Helvetica, sans-serif; font-si=
ze: 12pt; color: rgb(0, 0, 0);">
          <br>
        </div>
        <div class=3D"elementToProof"
style=3D"margin: 0px; font-family: Calibri, Helvetica, sans-serif; font-si=
ze: 12pt; color: rgb(0, 0, 0);">
          G.</div>
        <div class=3D"elementToProof"
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          <br>
        </div>
        <div class=3D"elementToProof"
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          -------------------- Code1 Starts -----------------------</div>
        <div class=3D"elementToProof"
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          import win32com.client as win32</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          import pythoncom</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          <br>
        </div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          #The event handlers</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          class wbEvents:</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          =C2=A0 =C2=A0 def OnBeforeClose(self, Cancel):</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          =C2=A0 =C2=A0 =C2=A0 =C2=A0 print('Closing Workbook')</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          =C2=A0 =C2=A0 =C2=A0 =C2=A0 # Send WM_QUIT here ?</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          <br>
        </div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          xlApp =3D win32.Dispatch("Excel.Application") #Open Excel</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          xlApp.Visible=3DTrue #Make Excel Visible</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: 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; col=
or: rgb(0, 0, 0);">
          ws=3Dxlwb.Sheets("Sheet1") #Get the first worksheet</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: 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; col=
or: rgb(0, 0, 0);">
          pythoncom.PumpMessages()</div>
        <div class=3D"elementToProof"
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          -------------------- Code1 Ends -----------------------</div>
        <div class=3D"elementToProof"
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          <br>
        </div>
        <div class=3D"elementToProof"
style=3D"text-align: left; text-indent: 0px; margin: 0px; font-family: Cal=
ibri, 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: Cal=
ibri, 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; col=
or: rgb(0, 0, 0);">
          import pythoncom</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          import time</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          <br>
        </div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          #The event handlers</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          class wbEvents:</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          =C2=A0 =C2=A0 def OnBeforeClose(self, Cancel):</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          =C2=A0 =C2=A0 =C2=A0 =C2=A0 print('Closing Workbook')</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          =C2=A0 =C2=A0 =C2=A0 =C2=A0 global keepOpen</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          =C2=A0 =C2=A0 =C2=A0 =C2=A0 keepOpen =3D False</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          <br>
        </div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          xlApp =3D win32.Dispatch("Excel.Application") #Open Excel</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          xlApp.Visible=3DTrue #Make Excel Visible</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: 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; col=
or: rgb(0, 0, 0);">
          ws=3Dxlwb.Sheets("Sheet1") #Get the first worksheet</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: 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; col=
or: rgb(0, 0, 0);">
          # define initalizer</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          keepOpen =3D True</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          while keepOpen:</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          =C2=A0 =C2=A0 time.sleep(0.1)</div>
        <div
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          =C2=A0 =C2=A0 pythoncom.PumpWaitingMessages()</div>
        <div class=3D"elementToProof"
style=3D"text-align: left; text-indent: 0px; margin: 0px; font-family: Cal=
ibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
          -------------------- Code2 Ends -----------------------</div>
        <div class=3D"elementToProof"
style=3D"font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; col=
or: rgb(0, 0, 0);">
          <br>
        </div>
        <br>
        <fieldset class=3D"moz-mime-attachment-header"></fieldset>
        <pre wrap=3D"" class=3D"moz-quote-pre">___________________________=
____________________
python-win32 mailing list
<a class=3D"moz-txt-link-abbreviated moz-txt-link-freetext"
        href=3D"mailto:[email protected]" moz-do-not-send=3D"true">p=
[email protected]</a>
<a class=3D"moz-txt-link-freetext"
        href=3D"https://mail.python.org/mailman/listinfo/python-win32"
        moz-do-not-send=3D"true">https://mail.python.org/mailman/listinfo/=
python-win32</a>
</pre>
      </blockquote>
      <br>
      <fieldset class=3D"moz-mime-attachment-header"></fieldset>
      <pre wrap=3D"" class=3D"moz-quote-pre">_____________________________=
__________________
python-win32 mailing list
<a class=3D"moz-txt-link-abbreviated" href=3D"mailto:[email protected]=
rg">[email protected]</a>
<a class=3D"moz-txt-link-freetext" href=3D"https://mail.python.org/mailman=
/listinfo/python-win32">https://mail.python.org/mailman/listinfo/python-wi=
n32</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>

--------------nFy5IsTXUwL240lm2vVeQ77y--

--===============3872627172263481679==
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

--===============3872627172263481679==--