I’m using docxtpl which uses jinja placeholder to set values.
For your solution, you should use jinja It’s used for whatever text files you want, htlm, txt, latex, MD, etc. And you can put code into the document (see my examples document in the post)