Home arrow Python arrow Page 4 - Windows Programming in Python

COM Programming in Python, in the Real World - 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

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
 

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: