Home arrow Python arrow Page 3 - Windows Programming in Python

Calling COM components from Python - Python

This article shows you how to call COM components from Python, and shows you how to use COM extensions to access the mail merge functionalities of MS Word in Python.

TABLE OF CONTENTS:
  1. Windows Programming in Python
  2. What is COM?
  3. Calling COM components from Python
  4. COM Programming in Python, in the Real World
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 26
April 19, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Now that the basis of COM is clear, it's time to see how to call COM components from the Python environment. This discussion will be focused on automation objects. I will be using the Word automation object as example. There are three major steps in calling a COM component from Python, which are:

1. Importing the win32 module.

2. Creating/launching the COM component.

3. Invoking methods on the object.

All of the above are the common steps to be followed in any type of programming. Still, while calling COM objects from the Python environment, the details of the steps are not that common.

1. Importing win32com package:

All the APIs required to work with the COM object are available in the win32com package. If COM automation objects have to be called, then the client module of the win32 module has to be used. If a server has to be created, then the server module has to be used. Hence to use COM automation objects, the import statement will be:

import win32com.client

2. Creating/launching the COM component:

As discussed in the previous section, COM components are registered with unique CLSIDs and human readable ProgIDs. For example, MS Word defines Word.Application as its ProgID. Using the ProgID, COM objects can be created. To use it with Python, the ProgID has to be passed to the Dispatch method of the client module. For example, to launch a Word automation object, the syntax would be:

import win32com.client
word = win32com.client.Dispatch("Word.Application")

word is now an object that represents MS Word. Now that we have an object of COM component, let's look at how to call the methods.

 3. Invoking methods on the object:

Before actually invoking methods of the MS Word object, it would be better to understand the binding that takes place while calling a method of COM object. There are two types of binding associated with COM components (as is usual in the object-oriented world): late binding and early binding.

With compiled languages, late binding refers to runtime binding of values to the attributes of the object, whereas early binding refers to the value/method resolution at compile time. However, as Python is a scripted language, there is a bit of a twist. 

Let's take a look at late binding. In the case of COM it is known as Late Bound Automation. It means that Python doesn't have knowledge of the property or method available for an object in advance. So whenever a property is referenced, the object is queried for that property. The following example will make this more clear. The Word automation object contains a property that decides the visibility of the object. The datatype is Boolean. So when Python sees this code:       

        word.Visible=1

it queries the word object to determine whether there is a property named visible. If it is there, its value would be set to 1. This is Late Bound Automation. And it is the default behavior of COM object launched by the Python environment. In the terminology of PythonCOM, which is the base package of PythonWin, this behavior is known as dynamic dispatch.

Now let's look at early binding. It is the opposite of late binding. If the supporting source packages of any COM object are available, then there is no requirement for dynamic dispatch. For this to happen, the PyMake utility has to be used. It generates Python source code supporting the COM interface using COM type library. The whys and wherefores will be discussed in the future. There is no change in semantics in the case of Early Binding. The only change would be in the internal working and improved execution.

To create a early-bound interface, start the PyMake from the win32com directory. Once started, it would present a list of objects MakePy can use to support early binding. Selecting the Word entry from the list and clicking the OK button would cause the support interface to be generated. The list presented by PyMake would be something like this:

 

So each COM object has its  own methods. For example,  to invoke the open method of word object, the statement would be:     

     word. Open(doc_template_name)

where doc_template_name is the name of the document to be opened. If Python source is available for the interface, it would be used. Otherwise dynamic dispatch would be used. That brings us to the end of this section. In the next section I will be applying the concepts and techniques discussed until now to create a MS Word Mail-Merge application using COM.         



 
 
>>> More Python Articles          >>> More By A.P.Rajshekhar
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: