Using Python to work with MS Word.

Gillou nospam at bigfoot.com
Fri Dec 20 12:03:20 EST 2002


"Anthony Evershed" <anthony.evershed at npl.co.uk> a écrit dans le message de
news: 2eb85cfb.0212200743.63504bf5 at posting.google.com...
> I have to push data (from an Oracle database or similar, accessed by a
> Python script, and hard-coded contents of variables in the Python
> script) into a document template (.dot file) in Microsoft Word 2000.
> As I am trying to keep as much of the transfer operated outside Word
> as possible, I need to use the Win32/COM interface features of Python
> to:
>
> a. Find the field tags in the template (for my own benefit, I've kept
> them to the form ---NameOfField---).
>
> b. Insert (by whatever means, but quite likely using the Paste command
> in Word.
>
> Does anyone have any ideas as to how to perform these tasks
> (especially the first, about which I have found no documation either
> in books or on the net?
>
> Thanks in advance,
>
> Anthony Evershed

Use Python 2.x.y (because most COM objects return Unicode strings).

1/ Download/install Mark Hammond's win32all (excellent) extension suitable
to your Python version

http://starship.python.net/crew/skippy/win32/Downloads.html

2/ Read the client Python COM doc that comes with that package. There are
examples in that doc, typically instaleld in...

C:\Python21\win32com\HTML\index.html

3/ Read the Word COM object model and API doc for Visual Basic and translate
into Python (pretty easy). Typically this doc resides in
C:\Program Files\Microsoft Office\Office\1036\WDMAIN9.CHM

Nearly all you cand do interactively (mouse, keyboard) with Word can be done
in a Python script.

HTH

--Gilles






More information about the Python-list mailing list