Editing/Writing Word-Files from Python

Hung Jung Lu hungjunglu at yahoo.com
Tue Apr 20 17:41:56 EDT 2004


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



More information about the Python-list mailing list