Python
  Home arrow Python arrow Page 3 - Windows Programming in Python
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Windows Programming in Python
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 24
    2006-04-19

    Table of Contents:
  • Windows Programming in Python
  • What is COM?
  • Calling COM components from Python
  • COM Programming in Python, in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Windows Programming in Python - Calling COM components from Python


    (Page 3 of 4 )

    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


       · COM is the basis of almost all of the window application. In one way or another we...
       · Seems easy. But I had a problem...this is the output I got from...
       · Which version of MS Office are you using
       · You have forgotten "Documents"Correct it has to be:...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway