SunQuest
 
       Python
  Home arrow Python arrow Page 4 - 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 
Moblin 
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 - COM Programming in Python, in the Real World


    (Page 4 of 4 )

    The application to be developed will take the advantage of early binding. So for the application to work efficiently, having the supporting interface generated would be a better choice. Moving on to the application, I will be developing the mail-merge application (without any GUI) by using the mail-merge functionality provided by the Word automation. The application requires the following to work:

    1. A doc template for Word.

    2. The addresses which have to be merged with the template  in CSV format

    3. The document where the merged document must be saved.

    Now let's look at the code. First, the imports:

    import os, win32com.client

    The os package is required to determine the absolute path of all the required files, which is done in the next statements path using the path module:

    import os, win32com.client

    doc_template_name = os.path.abspath('template.doc')
    data_source_name = os.path.abspath('record23.csv')
    doc_final_name = os.path.abspath('record23.doc')

    then create an object of Word application:

    import os, win32com.client

    doc_template_name = os.path.abspath('template.doc')
    data_source_name = os.path.abspath('record23.csv')
    doc_final_name = os.path.abspath('record23.doc')

    app = win32com.client.Dispatch("Word.Application")

    The next step is to obtain the document object by calling the open method of Documents object (which is contained within the returned app object) with the doc template's name.

    import os, win32com.client

    doc_template_name = os.path.abspath('template.doc')
    data_source_name = os.path.abspath('record23.csv')
    doc_final_name = os.path.abspath('record23.doc')

    app = win32com.client.Dispatch("Word.Application")
    doc_template = app.Documents.Open(doc_template_name)

    Next we must obtain reference to the MailMerge object, which provides the mail-merge functionality. Then we must open the data source for addresses using the OpenDataSource() method of the MailMerge object. And then we will merge the data source with the document. For brevity only one record is being merged:

    import os, win32com.client

    doc_template_name = os.path.abspath('template.doc')
    data_source_name = os.path.abspath('record23.csv')
    doc_final_name = os.path.abspath('record23.doc')

    app = win32com.client.Dispatch("Word.Application")
    doc_template = app.Documents.Open(doc_template_name)

    mm = doc_template.MailMerge

    #attach data source to template
    mm.OpenDataSource(data_source_name)

    #merge just one record - this step may be redundant
    mm.DataSource.FirstRecord = 1
    mm.DataSource.LastRecord = 1

    #send the merge result to a new document
    mm.Destination = win32com.client.constants.wdSendToNewDocument

    #merge
    mm.Execute()

    Before calling the execute method to merge the documents, the destination has to be specified, which is done using the COM  constant- win32com.client.constants.wdSendToNewDocument.

    Next we must obtain the reference of the newly merged document and then save it to the required file:

    import os, win32com.client

    doc_template_name = os.path.abspath('template.doc')
    data_source_name = os.path.abspath('record23.csv')
    doc_final_name = os.path.abspath('record23.doc')

    app = win32com.client.Dispatch("Word.Application")
    doc_template = app.Documents.Open(doc_template_name)

    mm = doc_template.MailMerge

    #attach data source to template
    mm.OpenDataSource(data_source_name)

    #merge just one record - this step may be redundant
    mm.DataSource.FirstRecord = 1
    mm.DataSource.LastRecord = 1

    #send the merge result to a new document
    mm.Destination = win32com.client.constants.wdSendToNewDocument

    #merge
    mm.Execute()

    #apparently app.Documents is like a stack
    doc_final = app.Documents[0]

    #save our new document
    doc_final.SaveAs(doc_final_name)

    #cleanup
    doc_final.Close()
    doc_template.Close()
    app.Quit()

    Once the task is done, the cleaning up has to be done by calling close on the final document object and doc template, apart from calling quit on the Word object to stop processing.

    That brings us to the end of this discussion. What I have discussed here is just the tip of the iceberg. What else can be achieved using win32com package will be discussed in the future. Till then...


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 6 hosted by Hostway