Mailing List Archive

stuck on COM interface to Word
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 is what I have so far:

from win32com.client import constants,Dispatch
import string,os

app = Dispatch('Word.Application.8')
app.Documents.Open(r"C:\My Documents\myMemo.doc")
doc = app.ActiveDocument
print "Last printed date: ", # ?? what goes here ??

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?

Thanks for the help!

Jim
--
Jim Kerr
Hewlett-Packard
Santa Rosa Systems Division
1400 Fountaingrove Pkwy, MS 3USZ
Santa Rosa, CA 95403
stuck on COM interface to Word [ In reply to ]
Hi Jim,
Here is an example:
import time
print time.ctime(int(doc.BuiltInDocumentProperties("Last Print Date")))
The online help contains other useful keywords(find the details of the
DocumentProperty): "Title","Subject","Author" etc.
Regards,
Arpad


Jim Kerr <jbkerr@sr.hp.com> wrote in message
news:<37A60515.370B5085@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 is what I have so far:
>
> from win32com.client import constants,Dispatch
> import string,os
>
> app = Dispatch('Word.Application.8')
> app.Documents.Open(r"C:\My Documents\myMemo.doc")
> doc = app.ActiveDocument
> print "Last printed date: ", # ?? what goes here ??
>
> 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?
>
> Thanks for the help!
>
> Jim
> --
> Jim Kerr
> Hewlett-Packard
> Santa Rosa Systems Division
> 1400 Fountaingrove Pkwy, MS 3USZ
> Santa Rosa, CA 95403
>
> --
> http://www.python.org/mailman/listinfo/python-list
>
stuck on COM interface to Word [ In reply to ]
In Arpad's example on *Last print date*, the returned <time object> is
converted to an integer using the function *int*. On my installation
I get the error: 'ValueError: illegal value'.

Should the *int* function be able to convert the <time object> and if
so - is there something I've missed out ?

Nikolai Kirsebom


On Tue, 3 Aug 1999 10:31:39 +0200, "Kiss, Arpad" <AKiss@GEOMETRIA.hu>
wrote:

>Hi Jim,
>Here is an example:
> import time
> print time.ctime(int(doc.BuiltInDocumentProperties("Last Print Date")))
>The online help contains other useful keywords(find the details of the
>DocumentProperty): "Title","Subject","Author" etc.
>Regards,
>Arpad
>
stuck on COM interface to Word [ In reply to ]
Nikolai Kirsebom wrote in message <37a6d12b.192256@news.mch.sni.de>...
>In Arpad's example on *Last print date*, the returned <time object> is
>converted to an integer using the function *int*. On my installation
>I get the error: 'ValueError: illegal value'.

This (probably) means the date is before 1970, the Python time module epoch.

>Should the *int* function be able to convert the <time object> and if
>so - is there something I've missed out ?

You can either use the builtin methods of the time object (documented in the
help file) or if you want to use dates seriously, get mxDateTime, which has
a function for converting from a COM date.

Mark.
stuck on COM interface to Word [ In reply to ]
Jim,

Jim Kerr <jbkerr@sr.hp.com> wrote in message
news:37A60515.370B5085@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@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@SAS.com