Editing/Writing Word-Files from Python

Daniel Cloutier danielcloutier at yahoo.com
Wed Apr 21 07:54:15 EDT 2004


Hung Jung Lu wrote:
> Daniel Cloutier <danielcloutier at yahoo.com> wrote in message news:<c63fgi$7esh7$1 at ID-47444.news.uni-berlin.de>...
> 
>>is it possible to edit or write Word-files out of a Python-Program?
> 
> 
> Yes. 
> 
> (a) You need the win32all modules from Mark Hammond.
> 
> (b) To try out the object model of MS Word, press ALT+F11 to bring up
> the VBA environment, then F2 to view the object browser. It's a
> complicated subject.
> 
> -------------------------------------------
> import pythoncom
> from win32com.client import Dispatch
> 
> app = Dispatch('Word.Application')
> app.Visible = 1
> doc = app.Documents.Add()
> s = doc.Sentences(1)
> s.Text = 'This is a test.'
> doc.SaveAs('C:\\mydoc2.doc')
> app.Quit()
> 
> app = None
> pythoncom.CoUninitialize()
> --------------------------------------------
> 
> You may want to add better exception handling, otherwise, you may
> often have danggling processes when exceptions happen. (You'll then
> have to manually kill them from the task manager.)
> 
> regards,
> 
> Hung Jung

thanks a lot, yesterday i found out myself that i have to use the 
win32com package, but i didn't know how to add some text

greetings
daniel



More information about the Python-list mailing list