stuck on COM interface to Word

Audun S. Runde Audun.Runde at SAS.com
Wed Aug 4 14:09:26 EDT 1999


Jim,

Jim Kerr <jbkerr at sr.hp.com> wrote in message
news:37A60515.370B5085 at sr.hp.com...
>   I'm trying to write a python script to extract some properties from a
set
> of Word documents. The attributes I'm interested in are things like
author's
> name, last print date, keywords, etc.

This should give you an idea, I hope:

PythonWin 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
Portions Copyright 1994-1999 Mark Hammond (MHammond at skippinet.com.au)
>>> from win32com.client import constants, Dispatch
>>> import string, os
>>> wd = Dispatch('Word.Application.8')
>>> wd.Documents.Open(r"c:\temp\test.doc")
<win32com.gen_py.Microsoft Word 8.0 Object Library.Document>
>>> doc = wd.ActiveDocument
>>> myprops = ("Author", "Subject", "Number of Words")
>>> for prop in myprops:
...  print prop, doc.BuiltInDocumentProperties(prop)
...
Author Audun S. Runde
Subject Test Subject
Number of Words 5
>>> print doc.CustomDocumentProperties("TestCustom")
Python Rules!
>>>


>   I tried using combrowse.py to figure out what the properties of the doc
are,
> but I couldn't find anything that looked relevant.
>
>   Is there any web-based info on COM interfaces to various programs? Or do
you
> have to buy Visual Basic books and translate for yourself?

To find out what to do here, you will have to cross over from Python to
Word/VBA. As another post mentioned, there is info about this in the online
Word help -- just make sure you explicitly installed the VBA help (it does
not come as part of a 'typical' install).

In your case, seach for BuiltInDocumentProperties in the online Visual Basic
Reference. To find the property names (i.e. 'Author', 'Subject', ...), seach
for DocumentProperty Object. These names also map to wdXXXX constants.

Generally, a good place to start is to turn on Word's Macro Recorder
(Tools->Macro-> Record New Macro...), do in Word what you'd like to
automate, then inspect the recorded macro for hints. In this case, however,
it doesn't work because accessing the Document Properties doesn't appear to
"do" anything in a macro.

A good place to look for more would be Microsoft Developer Network online at

    http://msdn.microsoft.com/default.asp

Hope this is useful :-)

Audun S. Runde
SAS Institute Inc.
Audun.Runde at SAS.com







More information about the Python-list mailing list