How Escaping works in docxtpl is given below. Following this will solve your issue.
By default, no escaping is done.
When you use a {{ }}, under the hood, you are modifying an XML word document, this means you cannot use all chars, especially <, > and &. In order to use them, you must escape them. There are 4 ways :
context = { 'var':R('my text') } and {{r <var> }} in the template (note the r),
context = { 'var':'my text'} and {{ <var>|e }} in your word template
context = { 'var':escape('my text')} and {{ <var> }} in the template.
enable autoescaping when calling render method: tpl.render(context, autoescape=True) (default is autoescape=False)
See tests/escape.py example for more informations.
Another solution, if you want to include a listing into your document, that is to escape the text and manage \n, \a, and \f you can use the Listing class
in your python code:
context = { 'mylisting':Listing('the listing\nwith\nsome\nlines \a and some paragraph \a and special chars : <>&') }
in your docx template just use {{ mylisting }}
With Listing(), you will keep the current character styling (except after a \a as you start a new paragraph).