Getting the contents of a Word document is pretty simple:

>>> import pythoncom
>>> from win32com.client import *
>>> w = Dispatch("Word.Application")
>>> doc = w.Documents.Open(r"C:\temp\testdoc.doc")
>>> doc
<win32com.gen_py.Microsoft Word 8.0 Object Library.Document>
>>> r = doc.Sections(1).Range
>>> r.Text
u'This is a test document.\rThis paragraph is bold.\rThis one is
italicized.\rThis one is mixed.\r\r'

  I had less success in trying to discover style information:
>>> p = doc.Paragraphs(2)
>>> p.Range.Text
u'This paragraph is bold.\r'
>>> firstchar = p.Range.Characters(1)
>>> firstchar.Text
u'T'
>>> firstchar.Style.Description
u'Font: Times New Roman, 10 pt, English (United States), Char scale 100%,
Flush left, Line spacing single, Widow/orphan control, Body text'

  This is either wrong or misleading, as the font is actually 32 point, not
10.
OTOH, I'm no expert at the Word model, so take this with a big grain of
salt.

  If you want to do any serious work with Python and Word, then
  1) Get a copy of the book "Python Programming on Win32" by Mark Hammond
and Andy
Robinson. A most excellent text.
  2) Find out more about the Word object model by poking around the VBA
editor
(Alt-F11, or Tools->Macro->Visual Basic Editor).


Jim
========
Jim Kerr
Software Engineer
Agilent Technologies, Inc.

> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED]]On Behalf
> Of Steven
> Cummings
> Sent: Thursday, October 18, 2001 10:03 AM
> To: [EMAIL PROTECTED]
> Subject: COM objects
>
>
> Hi,
>
> I'm trying to write a program that opens a word file and gets
> all of the text in a python string, and then closes it again.
> Preferably, I'd like to also be able to get the formatting
> info that goes with each "segment" of the text. I couldn't
> find any documentation on MSDN (I may have not searched long
> enought) for the object hierarchy of "Word.Application.9".
> Thanks ahead of time for any help. Later.
>
> /S
> --
>
> _______________________________________________
> Sign-up for your own FREE Personalized E-mail at Mail.com
> http://www.mail.com/?sr=signup
>
>
> Have you downloaded the latest calling software from
> Net2Phone? Click here to get it now!
>
> http://www.net2phone.com/cgi-bin/link.cgi?157
>
>
>
> _______________________________________________
> ActivePython mailing list
> [EMAIL PROTECTED]
> http://listserv.ActiveState.com/mailman/listinfo/activepython
>

_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activepython

Reply via email to