Python How To Update Word Documents Using Bookmarks

Do you like this?

This article explains how you can edit bookmarks in Word documents using Python.


This post is a continuation of the article Python Inserting Images Into Word Documents which shows you how to use PyWin32 to handle Word documents in Python. Hence, firstly you need to set up PyWin32 as shown in the article Python Inserting Images Into Word Documents.
The following code shows you how to handle bookmarks in Word documents in Python.

import win32com.client as win32 

wordApp = win32.gencache.EnsureDispatch('Word.Application') #create a word application object
wordApp.Visible = False # hide the word application
doc = wordApp.Documents.Open("template.doc")) # opening the template file
#for creating a new one: doc = wordApp.Documents.Add() 

rng=doc.Bookmarks("Name").Range # change the string Name to whatever name of your bookmarks
rng.InsertAfter("Hello World")



comments powered by Disqus