Thursday, July 30, 2009

Embedding Images in Word (Python/C#/VB)

I wrote a utility that exports trac wiki content into static html for the purposes of dumping it into a word document (for the powers that be). The Microsoft Word selection.InsertFile method turned out to be quite a lifesaver for this. My exporter utility does some hacking up of the urls so that bookmarks can be created for the hyperlinks. This makes it easy to navigate the word doc since the user can click the URLs in the doc and it will take them to the appropriate page/ paragraph of the content. It all worked out rather nicely, about 50 lines of python for the main word-doc-generation code. When all is said and done, I handed off the document to others to read and admire. After doing this, I was informed that all the images were missing. I found that hard to believe, since I could clearly see the images on my machine. So I looked into it and found out the InsertFile method does not cause images to be embedded within the word doc. As a result, the only machine the doc worked on was mine, since I had all the images within the docs folder..... nice. I could not find any nice one-stop method for telling the word doc to "save all images into this doc for me". That's a pain. I posted up an inquiry to Microsoft Discussion Groups and waited over night for an answer but nothing good came through. After waiting another day, a Jay Freedman gave me a tip. I ended up hacking it together and using the code provided below:

for shape in doc.InlineShapes:
if not shape.LinkFormat is None:
filename = shape.LinkFormat.SourceFullName
shape.LinkFormat.SavePictureWithDocument = True

Efficient or not, it works. With that being said, if any of you know of a cleaner way to accomplish this, please let me know!

No comments: