[python-win32] Python-win32 Digest, Vol 37, Issue 25

Daniel Greenfeld pydanny at gmail.com
Sat Apr 22 15:20:07 CEST 2006


> Date: Fri, 21 Apr 2006 10:00:33 -0700
> From: Tim Roberts <timr at probo.com>
> Subject: Re: [python-win32] Need help with AutoSummarize feature in
>         Word
> To: python-win32 at python.org
> Message-ID: <44490FB1.1060902 at probo.com>
> Content-Type: text/plain; charset=ISO-8859-1

First off, thanks very much Tim for the help.  Your advice made a big
difference!

> >It sometimes
> >neglects to close the word documents so then my computer gets loaded
> >with tons of open word documents
> >
>
> Right, because you close the summary, but you  never close the original
> document.

Gotcha.  I think I see what is going on via the issue of documents.

> ># COM constants that must be established
> >wdSummaryModeCreateNew  = 0x3
> >
> >
>
> That constant should be in win32com.client.constants after you do your
> EnsureDispatch.

Thanks for pointing that out.  Now I see how the EnsureDispatch works.

> >WORD                    = 'Word.Application'
> >False, True             = 0, -1
>
> That's an incredibly bad idea.  Python has intrinsic constants called
> False and True with very different values, and you should be able to use
> them with APIs that expect a Boolean.

I agree.  That was based off the sample I pulled out of the Learning
Python book.  Now I'ved nixed it and am much happer for it.

> Kind of a trivial note -- the + operator on strings is inefficient.
> It's better just to create the constants as one chunk, but since this is
> only a one-time thing, it really doesn't matter.

Don't worry about trivial notes.  The more advice I get from people on
proper Python practices the better.

> The Open API returns a Document object.  You should save that Document
> object, so that you can close it later.
>
>         self.original = self.app.Documents.Open( FileName = doc )

Thanks.  This simple bit of advice is really what has enabled me to
finish up the project two weeks ahead of schedule.

> >    def autoSummarize(self, Length = 30, Mode =
> >wdSummaryModeCreateNew, UpdateProperties = True):
> >        """ I do the autosummary and return the content.  This
> >actually creates a new tmp word file."""
> >        try:
> >            self.app.ActiveDocument.AutoSummarize(Length, Mode,
> >UpdateProperties)
> >
> AutoSummarize returns a Range object.  It may be possible to get the
> text directly from this Range object, instead of relying on the
> ActiveDocument property.

Can you help me with finding documentation or examples of different
ways to play with the Range objects?  The MSDN docs seem to be
assuming I already know what a Range is.

> >            return word.app.ActiveDocument.Content.Text
>
> You really want "self" instead of "word" here.  You're getting the
> global variable "word", which happens to be the same thing in this case,
> but better to do it right.

Oops!  Thanks!

> Here, you're closing the summary.  You also need to do:
>         self.original.Close()

Yep.  That works nicely!
 >
> Since you're running 4 different summaries from the same original, why
> not do the word.open outside of the loop?  You would have to change the
> Word class to close the autosummary document in the autoSummarize call,
> instead of in close, but that's easy.

It was easy.  Like I said, your advice made me jump really far ahead. 
Thanks so much!

--
'Knowledge is Power'
Daniel Greenfeld
http://del.icio.us/dgreenfe


More information about the Python-win32 mailing list