<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: AL Programmer</title>
    <description>The latest articles on Forem by AL Programmer (@darwinplatypus).</description>
    <link>https://forem.com/darwinplatypus</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1436976%2F9d114867-1849-45c9-818e-c2d6ce2f4df9.png</url>
      <title>Forem: AL Programmer</title>
      <link>https://forem.com/darwinplatypus</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/darwinplatypus"/>
    <language>en</language>
    <item>
      <title>One path to connecting a Python script to a COM application on Windows</title>
      <dc:creator>AL Programmer</dc:creator>
      <pubDate>Fri, 19 Apr 2024 18:42:05 +0000</pubDate>
      <link>https://forem.com/darwinplatypus/one-path-to-connecting-a-python-script-to-a-com-application-on-windows-38mb</link>
      <guid>https://forem.com/darwinplatypus/one-path-to-connecting-a-python-script-to-a-com-application-on-windows-38mb</guid>
      <description>&lt;p&gt;&lt;strong&gt;Background&lt;/strong&gt;&lt;br&gt;
I am an experienced software engineer having developed mostly embedded and distributed applications.  Most of the experience gained during my system design and development years were with embedded systems or Linux.  I have never developed anything for Windows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Situation&lt;/strong&gt;&lt;br&gt;
I decided to develop a Python application that could connect to a COM application.  Why?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I like Python because of the language’s flexibility and its vast community of solid contributions that allow us to not re-invent the wheel.&lt;/li&gt;
&lt;li&gt;Many compelling applications are Windows-based and, while this is changing, many are not available for other platforms.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That kicked off my interesting journey during which the best discoveries were about awesome people who freely contribute and give their time and expertise with kindness and nurture.  Especially in communities like WinPython where, for example, Mark Hammond is exemplary &lt;a href="https://www.oreilly.com/pub/au/59"&gt;https://www.oreilly.com/pub/au/59&lt;/a&gt; &amp;amp; &lt;a href="https://github.com/mhammond"&gt;https://github.com/mhammond&lt;/a&gt;.  This write up is in homage to them and their efforts. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I have a Windows 11 Pro license – I am a beginner-to-intermediate level user of Windows because I have used a Mac for over 20 years.&lt;/li&gt;
&lt;li&gt;I know nothing about developing on Windows: the logistics, the practice, the constructs, anything really.&lt;/li&gt;
&lt;li&gt;I hate to mess with the base OS, so I always prefer self-contained environments that do not alter any OS settings.  Hence my intense dislike for anything to do with, for example, editing the Windows Registry.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Objective&lt;/strong&gt;&lt;br&gt;
Develop a Python application that can communicate with a COM application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 1: Python on Windows&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;What to install&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Download and install WinPython from &lt;a href="https://winpython.github.io"&gt;https://winpython.github.io&lt;/a&gt;. I researched Python on Windows and in very short order understood that WinPython is the way to go.  While it’s stated audience is scientists, data scientists and education, it fully serves the needs of personal projects. Also, it is available as a portable distribution with no requirement to register with Windows. This checked all the boxes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;How to install&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Download 7-zip&lt;/li&gt;
&lt;li&gt;Download WinPython to the Downloads / your folder of choice&lt;/li&gt;
&lt;li&gt;Extract WinPython&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Assuming the distro was extracted to Downloads, use File Explorer to navigate to:&lt;br&gt;
&lt;code&gt;C:\Users\&amp;lt;username&amp;gt;\Downloads\WPy64-31220\&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Double-click to launch WinPython Command Prompt&lt;/p&gt;

&lt;p&gt;This brings up a command prompt shell with all the right environment variables set.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;How to run Python&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
At this command prompt you can run the Python interpreter using:&lt;br&gt;
&lt;code&gt;C:\Users\&amp;lt;username&amp;gt;\Downloads\WPy64-31220\python-3.12.2.amd64\python.exe&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note: the inner folder will be named per your processor architecture.&lt;/p&gt;

&lt;p&gt;Change directory to the “scripts” folder for the next few steps&lt;br&gt;
&lt;code&gt;cd C:\Users\&amp;lt;username&amp;gt;\Downloads\WPy64-31220\scripts&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Make sure pip is installed and updated&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;upgrade_pip.bat&lt;br&gt;
pip install --upgrade pip&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Install the pywin32 package (and any others you want)&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;pip install --upgrade pywin32&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At this point you are setup with Python and the required lower-level pywin32 package.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 2: Creating Python classes to interface with the target COM application&lt;/strong&gt;&lt;br&gt;
The next step deals with the fact that while WinPython and other modules may understand the COM data structures for widely deployed applications, e.g. Excel, they won’t understand the COM data structures for your target COM application.  And they need to model those data structures as Python classes.  So, your next step is to generate these Python classes. (Note: the preceding description is likely not entirely accurate but my intent is to describe the gist of the need.)&lt;/p&gt;

&lt;p&gt;Change directory:&lt;br&gt;
&lt;code&gt;cd C:\Users\&amp;lt;username&amp;gt;\Downloads\WPy64-31220\python-3.12.2.amd64\Lib\site-packages\win32com\client&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Generate the required COM classes by running makepy&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;C:\Users\&amp;lt;username&amp;gt;\Downloads\WPy64-31220\python-3.12.2.amd64\python.exe makepy.py&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will pop up a Window that allows you to select the target registered program.  Select your program and click OK.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Confirm the required COM classes have been successfully generated by running combrowse&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;C:\Users\&amp;lt;username&amp;gt;\Downloads\WPy64-31220\python-3.12.2.amd64\python.exe combrowse.py&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will pop up a browser that you can navigate to confirm the target classes have been generated.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 3: Connecting to the target COM application in your Python script&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;import win32, win32com.client, pythoncom&lt;br&gt;
myCOMAppHandle = win32com.client.DispatchEx (&amp;lt;name of COM App specified as a string&amp;gt;);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 4: Calling the COM application&lt;/strong&gt;&lt;br&gt;
NOTE: The code below is likely specific to my target COM, application – your application interface may differ&lt;br&gt;
&lt;code&gt;z = win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_R8, -6.2);&lt;br&gt;
funcReturnCode = myCOMAppHandle.someAttr.someMethod(x, y, z)&lt;/code&gt;&lt;br&gt;
where x, y are input parameters, funcReturnCode is the code returned by the target COM application, and z is a return variable for data returned by the call.&lt;/p&gt;

&lt;p&gt;That's it!&lt;/p&gt;

</description>
      <category>python</category>
      <category>windows</category>
      <category>windowscom</category>
    </item>
  </channel>
</rss>
