Package: docxtpl
Use a docx as a jinja2 templateIntroductionThis package uses 2 major packages :python-docx for reading, writing and creating sub documentsjinja2 for managing tags inserted into the template docxpython-docx-template has been created because python-docx is powerful for creating documents but not for modifying them.The idea is to begin to create an example of the document you want to generate with microsoft word, it can be as complex as you want :
pictures, index tables, footer, header, variables, anything you can do with word.
Then, as you are still editing the document with microsoft word, you insert jinja2-like tags directly in the document.
You save the document as a .docx file (xml format) : it will be your .docx template file.Now you can use python-docx-template to generate as many word documents you want from this .docx template and context variables you will associate.DocumentationPlease,read the docOther projectsIf you like python-docx-template, please have a look at some of my other projects :django-listing: A listing/table library on steroid for Djanopython-textops3: Chainable text operationsdjango-robohash-svg: Create svg robots avatarsNews0.16.8 (2024-02-23)PR #527 : upgrade Jinja2 in Pipfile.lock0.16.7 (2023-05-08)PR #493 - thanks to AdrianVorobel0.16.6 (2023-03-12)PR #482 - thanks to dreizehnutters0.16.5 (2023-01-07)PR #467 - thanks to Slaragfix #465fix #4640.16.4 (2022-08-04)Regional fonts for RichTextReorganize documentation0.16.3 (2022-07-14)fix #4480.16.2 (2022-07-14)fix #444fix #4430.16.1 (2022-06-12)PR #4420.16.0 (2022-04-16)add jinja2 comment support - Thanks to staffanm0.15.2 (2022-01-12)fix #408Multi-rendering with same DocxTemplate object is now possible
see tests/multi_rendering.pyfix #392fix #3980.14.1 (2021-10-01)One can now use python -m docxtpl on command line
to generate a docx from a template and a json file as a context
Thanks toLcrs123@github0.12.0 (2021-08-15)Code has be split into many files for better readabilityUse docxcomposer to attach parts when a docx file is given to create a subdoc
Images, styles etc… must now be taken in account in subdocsSome internal XML IDs are now renumbered to avoid collision, thus images are not randomly disapearing anymore.fix #372fix #374fix #375fix #369fix #368fix #347fix #181fix #610.11.5 (2021-05-09)PR #351It is now possible to put InlineImage in header/footerfix #323fix #320\n, \a, \t and \f are now accepted in simple context string. Thanks tochabErch@github0.10.5 (2020-10-15)Remove extension testing (#297)Fix spaces missing in some cases (#116, #227)0.9.2 (2020-04-26)Fix #271Code styling0.8.1 (2020-04-14)fix #266docxtpl is now able to use latest python-docx (0.8.10). Thanks toDutchy-@github.0.7.0 (2020-04-09)Add replace_zipname() method to replace Excel and PowerPoint embedded files0.6.4 (2020-04-06)Add the possibility to add RichText to a RichtextPrevent lxml from attempting to parse NonePR #207 and #209Handle spaces correctly when run are split by Jinja code (#205)PR #203DocxTemplate now accepts file-like objects (Thanks to edufresne)0.5.20 (2019-05-23)Fix #199Add support for file-like objects for replace_media (#197)Fix  #176Delegated autoescaping to Jinja2 Environment (#175)Force to use python-docx 0.8.7 (#170)Add getting undeclared variables in the template (#171)AddedPAGE_BREAKfeature (#168)Fixed issue #159: autoescaped values for both str and unicode.Fix tables with gridSpan that have less cells after the tc forloop (#164)Smart double quotes in jinja tags are now converted into simple double quotesSmart quotes in jinja tags are now converted into simple quotesAdd custom jinja filter example in tests/Reformat the code to be a little more PEP8 compliantAdd {% hm %} tag for table columns horizontal merging (Thanks to nickgashkov)Split tests/tests_files dir into templates and output dirsautoescape support for python 2.7fix issue #154Render can now autoescape context dictFix invalid xml parse because using {% vm %}Cast to string non-string value given to RichText or Listing objectsImport html.escape instead of cgi.escape (deprecated)Declare package as python2 and python3 compatible for wheel distribAdd sub/superscript in RichTextFix table vertical mergeAn hyperlink can now be used in RichText0.4.13 (2018-06-21)Subdocument can now be based on an existing docxAdd font option in RichTextBetter tabs and spaces management for MS Word 2016Wheel distributionManage autoscaping on InlineImage, Richtext and SubdocPurge MANIFEST.in fileAccept variables starting with ‘r’ in {{}} when no space after {{Remove debug tracesAdd {% vm %} to merge cell vertically within a loop (Thanks to Arthaslixin)use six.iteritems() instead of iteritems for python 3 compatibilityFixed Bug #95 on replace_pic() methodAdd replace_pic() method to replace pictures from its filename (Thanks to Riccardo Gusmeroli)Improve image attachment for InlineImage ojectsAdd replace_media() method (useful for header/footer images)Add replace_embedded() method (useful for embedding docx)0.3.9 (2017-06-27)Fix exception in fix_table()Fix bug when using more than one {{r }} or {%r %} in the same runFix git tag v0.3.6 was in fact for 0.3.5 package version
so create a tag 0.3.7 for 0.3.7 package versionBetter head/footer jinja2 handling (Thanks to hugokernel)Fix bug where one is using ‘%’ (modulo operator) inside a tagAdd Listing class to manage n and a (new paragraph) and escape text AND keep current stylingAdd {%tc } tags for dynamic table columns (Thanks to majkls23)Remove version limitation over sphinx package in setup.pyAdd PNG & JPEG in tests/test_files/You can now add images directly without using subdoc, it is much more faster.0.2.5 (2017-01-14)Add dynamic colspan tag for tablesFix /n in RichText classAdd Python 3 support for footer and headerFix bug when using utf-8 chracters inside footer or header in .docx template
It now detects header/footer encoding automaticallyFix bug where using subdocs is corrupting header and footer in generated docx
Thanks to Denny Weinberg for his help.Add Header and Footer support (Thanks to Denny Weinberg)0.1.11 (2016-03-1)‘>’ and ‘<’ can now be used inside jinja tagsrender() accepts optionnal jinja_env argument :
useful to set custom filters and other thingsbetter subdoc management : accept tablesbetter xml code cleaning around Jinja2 tagspython 3 supportremove debug codeadd lxml dependencyfix template filter with quoteadd RichText supportadd subdoc supportadd some exemples in tests/First running version