Do you have Word templates, which need to be updated regularly from a certain source? Tired of doing this manually and want to automate the task, via python for example? Then this article is for you. Please note that the focus will not be made on the first step (Link to the database). Here, we are assuming that the data are made available to the code, which use them to update the template automatically. This part should be resolved from you and the data should be available for the code. If you have problems with this step, please let a comment, so that we can extend this article, if needed.
The following solution is based on Python. Please notice that this solution can be written in any other language. The only thing you need is to re-write the proposed algorithm in the desired language ;-). This solution is composed by these three principal steps.
Create a Microsoft Word template ready for automation
Actually, you need to use some objects in the Word template, which can be controlled from outside of Micosoft Word. I would suggest using “rich text” object.
In the template, you can give the rich text object a name and a tag, so that it can be found and identified.
You could have a template in the following form, where the objects are named and tagged.
You can also have some bookmarks, so that you can reuse the given information (names) for example, via cross-references. To do so, you need to create the bookmarks
Then insert the cross-reference, where you need it, as illustrated in the figures below:
Create a (python) script to fill the template
As already stated, reading the data from the source is out of the scope of this article and will not be detailed. The following python code lines can be used to perform the filling of the Word template automatically. Comments are inserted in the code to explain the purpose of each part of the code.
To perform the automatic writing, you can use the following code. Please note that you can of course download the template and the code by clicking on the link below
''' Created on 16.04.2022 @author: YK ''' import win32com.client as win32 ''' give the path to the template''' Template_path = 'C:\Data\Template.docx' ''' set a path where the filled template will be saved''' path_saved_doc = 'C:\Data\saved_doc.docx' ''' give the names, with which the template will be filled''' data = ['Petra','Dollar'] ''' give the tags of the objects in the template, which would be filled''' Tag_list = ['firstname', 'lastname'] try: try: ''' Establish a connection to the Word application''' appli = win32.gencache.EnsureDispatch('Word.Application') except Exception as e3: print('Error: execution failed - Connection to COM via win32.gencache.EnsureDispatch(Word.Application) cannot be established') raise(e3) appli.Visible = False ''' open the document so that the filling can be started''' doc = appli.Documents.Open(Template_path) ''' loop over the tags and fill them respectively, with the names given in data''' for i in range(0,len(Tag_list)): try: ''' catch the text plain object Tag_list[i]''' rng = doc.SelectContentControlsByTag(Tag_list[i]).Item(1).Range except Exception as e: #print(e) print(Tag_list[i] + " cannot be retrieved from " + Template_path) continue '''get the names given in data''' Tex = data[i] '''write the name given in data in the template''' rng.Text = Tex '''If bookmarks are used, it is appropriate to think about recovering them. The writing process can unfortunately result in deleting them''' if not not rng: rng.Text = rng exec("doc.Bookmarks.Add(\"Bookmark_name_" + str(i) +"\",rng)") ''' This part is useful to update cross references related to used Bookmarks, if they are used in the document''' doc.Fields.Update() doc.PrintPreview() doc.ClosePrintPreview() ''' This part will save the filled document''' doc.SaveAs(path_saved_doc) except Exception as e3: print('Error: execution failed') print(e3) ''' close the document and the word object''' doc.Close(False) appli.Quit() print('----------------execution finished------------------')
The execution of the code will result in creating the following document