Jump to content


Photo

Python & COM

Python COM Common Object Model

  • Please log in to reply
No replies to this topic

#1 Neil

Neil

    Administrator

  • Administrators
  • 1,493 posts

Posted 20 January 2013 - 12:33 PM

Erm... what should I say here!
 
A long time ago in a galaxy far far away, The evil Microsoft empire wanted to create away for programs to talk to each other without needing to know all the specific details of how there were created, and so COM was developed to rule them all. Well I'm mixing my film metaphors but you get the picture!
 
COM or the "Common Object Model" enables us to conveniently interface to other programs and this is exactly how are scripts are working and are able to call functions / object within programs such as MaxIm. 
 
To open your eyes to this new world of COM objects, if you managed to follow all of the previous steps then please open PythonWin go the Tools and open the COM Browser, now take a look at the Registered Type Libraries... All these COM function and object have been registered by the programs that you have installed.
 
 
Below is an extract taken from the internet.
 
http://oreilly.com/c...apter/ch12.html Python and COM
The interface between Python and COM consists of two discrete parts: the pythoncom Python extension module and the win32com Python package. Collectively, they are known as PythonCOM.

The pythoncom module is primarily responsible for exposing raw COM interfaces to Python. For many of the standard COM interfaces, such as IStream or IDispatch, there is an equivalent Python object that exposes the interface, in this example, a PyIStream and PyIDispatchobject. These objects expose the same methods as the native COM interfaces they represent, and like COM interfaces, do not support properties. The pythoncom module also exposes a number of COM-related functions and constants.

The win32com package is a set of Python source files that use the pythoncom module to provide additional services to the Python programmer. As in most Python packages, win32com has a number of subpackages; win32com.client is concerned with supporting client-side COM (i.e., helping to call COM interfaces), and win32com.server is concerned with helping Python programs use server-side COM (i.e., implement COM interfaces). Each subpackage contains a set of Python modules that perform various tasks.
Using Automation Objects from Python

As we discussed previously, automation objects are COM objects that expose methods and properties using the IDispatch interface. So how do we use these objects from Python? The win32com.client package contains a number of modules to provide access to automation objects. This package supports both late and early bindings, as we will discuss.
To use an IDispatch-based COM object, use the method win32com.client.Dispatch(). This method takes as its first parameter the ProgID or CLSID of the object you wish to create. If you read the documentation for Microsoft Excel, you'll find the ProgID for Excel isExcel.Application, so to create an object that interfaces to Excel, use the following code:

 

To be typed into your interpreter window.

>>> import win32com.client
>>> xl = win32com.client.Dispatch("Excel.Application")
>>>







Also tagged with one or more of these keywords: Python, COM, Common Object Model