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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PYTHON

Windows Programming in Python
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 26
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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...



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

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek