[python-win32] inserting an image in a word (2003) document

Stef Mientki stef.mientki at gmail.com
Tue May 31 23:22:50 CEST 2011


I have word document,
with special words like "$$$Name$$$",
which are replaced with values from a database.

If the special word starts with an underscore, like "$$$_Example$$$",
the text should be replaced by an image.

I use the code below, which works quit well for texts,
but images, just the filename of that image is inserted.
Does anyone has a clue, what I'm doing wrong ?

self.app = win32com.client.Dispatch ( "Word.Application" )
self.app.Visible = visible

Find all occurances of 'find_str' and replace with 'replace_str'
in the active document.
self.app.Selection.HomeKey ( Unit = wdStory )
find = self.app.Selection.Find
find.Text = find_str
while self.app.Selection.Find.Execute():
if find_str.startswith ( '_' ) :
#expression.AddPicture(FileName, LinkToFile, SaveWithDocument, Range)
#self.app.Selection.InlineShapes.AddPicture ( FileName = replace_str )
#self.app.Selection.InlineShapes.AddPicture ( FileName = replace_str, LinkToFile = True )
self.app.Selection.InlineShapes.AddPicture ( FileName = replace_str, SaveWithDocument = True )

else :
self.app.Selection.TypeText ( Text = replace_str )


More information about the python-win32 mailing list