Use a docx as a jinja2 template

Overview

python-docx-template

Use a docx as a jinja2 template

Introduction

This package uses 2 major packages :

  • python-docx for reading, writing and creating sub documents
  • jinja2 for managing tags inserted into the template docx

python-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.

Share

If you like this project, please rate and share it here : http://rate.re/github/elapouya/python-docx-template

Documentation

Please, read the doc

Other projects

Have a look at some of my other projects :

Issues
  • how can i add another docx file to a existed docx file?

    how can i add another docx file to a existed docx file?

    there have tow docx files,named A.docx and B.docx,and now,I want to add A.docx to the B.docx,not the content of the A.docx, it just like a link,a attachemnt inside in B.docx,but question is how can i do it? please help me, thanks!!

    Request for enhancement 
    opened by WUWEIREN 31
  • ValueError: can only parse strings

    ValueError: can only parse strings

    I installed docxtpl on a clean machine today. I ran some code that used to work but couldn't generate any document.

    The exact same exception is raised when running the basic usage code from the documentation.

    from docxtpl import DocxTemplate
    
    doc = DocxTemplate("my_word_template.docx")
    context = { 'company_name' : "World company" }
    doc.render(context)
    doc.save("generated_doc.docx")
    

    Using Python 3.7.1

    Traceback:

    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-4-70cd50864075> in <module>
    ----> 1 doc.render(context)
          2 doc.save("generated_doc_.docx")
    
    /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/docxtpl/__init__.py in render(self, context, jinja_env, autoescape)
        297         headers = self.build_headers_footers_xml(context, self.HEADER_URI,
        298                                                  jinja_env)
    --> 299         for relKey, xml in headers:
        300             self.map_headers_footers_xml(relKey, xml)
        301 
    
    /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/docxtpl/__init__.py in build_headers_footers_xml(self, context, uri, jinja_env)
        245 
        246     def build_headers_footers_xml(self,context, uri,jinja_env=None):
    --> 247         for relKey, xml in self.get_headers_footers_xml(uri):
        248             encoding = self.get_headers_footers_encoding(xml)
        249             xml = self.patch_xml(xml)
    
    /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/docxtpl/__init__.py in get_headers_footers_xml(self, uri)
        236         for relKey, val in self.docx._part._rels.items():
        237             if val.reltype == uri:
    --> 238                 yield relKey, self.xml_to_string(parse_xml(val._target._blob))
        239 
        240     def get_headers_footers_encoding(self,xml):
    
    /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/docx/opc/oxml.py in parse_xml(text)
         35     ``etree.fromstring()`` replacement that uses oxml parser
         36     """
    ---> 37     return etree.fromstring(text, oxml_parser)
         38 
         39 
    
    src/lxml/etree.pyx in lxml.etree.fromstring()
    
    src/lxml/parser.pxi in lxml.etree._parseMemoryDocument()
    
    ValueError: can only parse strings
    

    I tried to investigate, maybe it's related to this URI being unreachable, but I'm not sure at all https://github.com/elapouya/python-docx-template/blob/2fad92046e580b6e9a212647f274d5efaae82648/docxtpl/init.py#L41

    opened by gggdomi 23
  • Richtext Styles Not being Applied

    Richtext Styles Not being Applied

    Hi, just wanted to say that your lib is fantastic, but i appear to be having an issue. It appears that if the style contains a space or certain characters, the style isnt applied in the output file. I was just wondering if there was a work around for this? Many Thanks. Example below: rt.add(tag.text , style='Subtle Reference')

    opened by HolyChute 17
  • Simple problem with expressions (math and strings)

    Simple problem with expressions (math and strings)

    Hi,

    I am having a problem where while this works inside a template file:

    {{ '\t\t\t\t\t\t\t\t\t\t' }} Test

    This doesn't:

    {{ 10*'\t' }} Test

    I get only just Test, with no space from the left margin at all.

    Does anyone know why? Thanks!

    opened by Sup3rGeo 16
  • base64 images

    base64 images

    Hi,

    Is it possible to use base64 encoded images with InlineImage?

    I have tried this kind of implementation (and many others) but It raises me a UnrecognizedImageError exception:

    img = io.StringIO(base64_str_image)
    image = InlineImage(doc, img)
    

    Thanks a lot! :)

    opened by QuentinBrosse 15
  • Template syntax error unexpected char '

    Template syntax error unexpected char '

    When i try to use a custom filter with a string argument like this {{ myvalue|myfilter('myargument') }} i get an error

    jinja2.exceptions.TemplateSyntaxError:unexpected char '''.

    Please help with an example of custom filter which shows how to pass a string argument. Integer arguments are passed without issues.

    I have later discovered while going through other reported issues, that this is due to the fact that the straight quotes ' are automatically converted to curly quotes ‘ which are not identified by jinja as valid character. While pasting the quote from another editor like atom works but this does not look like a good solution. One has to modify the autoreplace default setting of word which automatically changes straight quotes to curly quotes.

    Has any body tried the patch_xml() fix as reported in this thread https://github.com/elapouya/python-docx-template/issues/85

    Pasting the code at line 47 does not help me.

    opened by sandeeprah 14
  • For loop works only once

    For loop works only once

    Loop iterating over list of dicts, insert only first one item, and breaks the generation (part after loop not inserted)

    image image

    Using russian, if it's matter

    Using this template: template.docx, i got this: report3.docx

    opened by dadyarri 13
  • ValueError: target_part property on _Relationship is undefined

    ValueError: target_part property on _Relationship is undefined

    I used docxtpl v0.6.3 in order to generate 3 docx based on 3 templates. One template contains headers and footers

    Since the update to v0.8.0 (where you use python-docx (0.8.10)), generating that third template gives the following error:

    File "C:\Users\xxx\Desktop\xxx.py", line 476, in <module>
        doc.render(context)
      File "C:\Program Files\Python37\lib\site-packages\docxtpl\__init__.py", line 294, in render
        self.map_headers_footers_xml(relKey, xml)
      File "C:\Program Files\Python37\lib\site-packages\docxtpl\__init__.py", line 266, in map_headers_footers_xml
        new_part.load_rel(rel.reltype, rel.target_part, rel.rId, rel.is_external)
      File "C:\Program Files\Python37\lib\site-packages\docx\opc\rel.py", line 161, in target_part
        raise ValueError("target_part property on _Relationship is undef"
    ValueError: target_part property on _Relationship is undefined when target mode is External
    

    Can this be solved here, or is it a python-docx issue ?

    opened by Banaanhangwagen 12
  • Losing spaces before cross reference to table

    Losing spaces before cross reference to table

    In some cases a single or even double space is lost before a cross reference to a table. In the template document it is there, in the exported document it is gone.

    opened by arieknarie 11
  • UnicodeDecodeError

    UnicodeDecodeError

    opened by roxmiv 11
  • Accepting docx as file-like object, and saving to file-like object

    Accepting docx as file-like object, and saving to file-like object

    python-docx accepts a file path to a docx file, or a file-like object in it's constructor. It also allows saving to a file-like object.

    As far as I can tell, python-docx-template only accepts the file path.

    I have a requirement to do template a docx in memory. Is the file-like object supported?

    help wanted 
    opened by bgswe 0
  • Issues with Fetching Docx Attributes and `__getattr__` with v0.15.x

    Issues with Fetching Docx Attributes and `__getattr__` with v0.15.x

    Hey there, I appreciate all the work on this project! I was making some updates today and tried out 0.15.2. I ran into some odd issues, so I looked through the code changes. This commit introduced issues for accessing attributes:

    589262664f30890e58e8cabbec0c80321f4ccdf2

    Specifically, it's that line I bookmarked. It changed from self.docx = Document(docx) to self.docx = None. The docx variable is used with __getattr__() to fetch attributes of the underlying Docx object.

    https://github.com/elapouya/python-docx-template/blob/master/docxtpl/template.py#L56

    Starting with v.015.1, self.docx is not set until init_docx() is called. That method isn't called until the document is being rendered with render() or you call get_docx().

    This means attributes of the underlying Docx object and template file cannot be checked prior to rendering and attempts to do so will cause __getattr__() to raise an AttributeError. For example, it can be helpful to check a template file to see if expected styles are available.

    You can reproduce and test this with this Python code:

    from docxtpl import DocxTemplate
    
    template_document = DocxTemplate(FILE_PATH)
    for x in template_document.styles:
        print(x)
    

    With 0.14.1, that will output the template files styles (e.g., _ParagraphStyle('Heading 1') id: 4504633504). With 0.15.x, this code will throw an AttributeError when __getattr__() tries to get the attribute of self.docx because it's still NoneType.

    Traceback (most recent call last):
      File "docxtest.py", line 5, in <module>
        for x in template_document.styles:
      File "/usr/local/lib/python3.9/site-packages/docxtpl/template.py", line 57, in __getattr__
        return getattr(self.docx, name)
    AttributeError: 'NoneType' object has no attribute 'styles'
    

    I might be missing something, but it looks like the change to that line can just be reversed. It doesn't look like there is a benefit gained from self.docx being None when a new DocxTemplate is instantiated.

    You can call get_docx() to set self.docx. That's a solution, but also supports the reasoning that self.docxcould be set ininit(). You can adjust the above example to useget_docx()` and avoid the issue:

    from docxtpl import DocxTemplate
    
    template_document = DocxTemplate(FILE_PATH)
    template_document.get_docx()
    for x in template_document.styles:
        print(x)
    

    Happy to provide more info or help where I can.

    bug 
    opened by chrismaddalena 0
  • AttributeError: 'InlineImage' object has no attribute 'current_rendering_part'

    AttributeError: 'InlineImage' object has no attribute 'current_rendering_part'

    Please explain something about this error. I am getting this when I try to render my JSON. There are multiple InlineImage objects in that JSON.

    opened by rishi1804 0
  • Rendering with merge_subdoc and then text variables.

    Rendering with merge_subdoc and then text variables.

    Problem: I want to create a docx that merges subdocs based on given inputs(docx has x, y, and z sections from merged subdocs that are separate docx files), and then populates variables(name, pronoun, etc.) throughout the entire docx. I can get each to work separately(name is inserted throughout template); or subdocs are merged into template and saved. When I try and have subdocs merged, and then variables populated(so that the tags in the subdocs are filled with appropriate info) the subdocs will merge, but no tags are rendered.

    How to create docx from template that merges subdocs, and then autoreplaces all input variables?

    Attempted: I have tried to add all subdocs needed and text variables into a single dictionary and then rendering. I have tried to render subdocs first, but only the 1st subdoc gets merged, and then all text variables are replaced with white space.

    Code: `for variable in ["firstName", "lastName"]: demographics[variable] = eval(variable)

    report.render(demographics)

    subdocinput = report.new_subdoc('subdocfile.docx') subdocinput2 = report.new_subdoc('subdocfile2.docx')

    for tests in {"subdocinput", "subdocinput2"}: assessments[tests] = eval(tests) report.render(assessments)`

    help wanted 
    opened by homerboy22 0
  • Update template.py

    Update template.py

    Auto generate variables dictionary by jinja2schema

    opened by ZChidori 1
  • Banded rows when looping through a table

    Banded rows when looping through a table

    Hello

    I wanted to confirm if there was a way to make sure the banded rows (rows in alternate colors) show up.

    In my example.pdf, the table at the top has the banded rows option selected (under the Table Design tab).

    However, when looping through the table, only the orange color row (or any other color that the variable is in) is preserved and you don't get the appearance of the banded row.

    Would like some help on how I could achieve this. Thanks.

    help wanted 
    opened by nixshal 1
  • Generating Word equation

    Generating Word equation

    Hi folks,

    I am trying to generate a Word equation using MathML. Basically, I inject some MathML code in a variable then creating an alias to this variable in my template. However, this does not show up as a Word equation, instead we see the MathML code. The MathML seems to be copied as "Unformatted Unicode Text". Would you have an idea how I could deal with that please ?

    Many thanks !

    Request for enhancement 
    opened by Mill7 3
  • Can i pass instance model django to docxtpl

    Can i pass instance model django to docxtpl

    i using docxtpl with django project but i cannot pass instance to docxtpl that showing error "'MyInstance' object is not iterable"

    cause of why i use instance . i have a many relationship such as ForeignkeyField ManytoManyField and i have a model method for calculate something in model. if i covert to dict model method is not working.

    help me pls.

    help wanted 
    opened by dirosv-eden 1
  • How to set font spacing?

    How to set font spacing?

    https://github.com/python-openxml/python-docx/issues/243,thanks!!!

    Request for enhancement 
    opened by yszzQ 1
  • Retrieve the variables being used in the template

    Retrieve the variables being used in the template

    I have a template docx file which is using three variables

    e.g There is a paragraph that uses 3 variables as shown below.

    {{ a }} this is variable a. and the variable {{b}} will be applied at {{c}} % p.a

    Is there any way I can retrieve the list of variables [a,b,c] being used in the template file? Something like this - https://www.phpdocx.com/api-documentation/templates/get-Word-template-variables

    Thanks

    help wanted 
    opened by yogesh-chaudhari-77 2
Owner
Eric Lapouyade
Eric Lapouyade
✈️ HTML Template engine for python. Supports XSS preventation and many more!

Htmotor HTML Template Engine for Python! Installation: Open your terminal and type pip install htmotor.

Penguen 4 Aug 29, 2021
Simple reuse of partial HTML page templates in the Jinja template language for Python web frameworks.

Jinja Partials Simple reuse of partial HTML page templates in the Jinja template language for Python web frameworks. (There is also a Pyramid/Chameleo

Michael Kennedy 51 Jan 24, 2022
A general purpose template driven code generator

?? Shit Mountain Generator A general purpose template driven code generator Contribute shits to your company's shit mountain more efficiently! Quick S

Kelly 12 Dec 21, 2021
A string template language hosted by Python3 runtime

A string template language hosted by Python3 runtime. Conventionally, the source code of this language is written in plain text with utf-8 encoding and stored in a file with extension ".meme".

null 3 Jan 28, 2022
Use a docx as a jinja2 template

Use a docx as a jinja2 template

Eric Lapouyade 1.2k Feb 6, 2022
HTTP(s) "monitoring" webpage via FastAPI+Jinja2. Inspired by https://github.com/RaymiiOrg/bash-http-monitoring

python-http-monitoring HTTP(s) "monitoring" powered by FastAPI+Jinja2+aiohttp. Inspired by bash-http-monitoring. Installation can be done with pipenv

itzk 34 Feb 13, 2022
A Python command-line utility for validating that the outputs of a given Declarative Form Azure Portal UI JSON template map to the input parameters of a given ARM Deployment Template JSON template

A Python command-line utility for validating that the outputs of a given Declarative Form Azure Portal UI JSON template map to the input parameters of a given ARM Deployment Template JSON template

Glenn Musa 1 Feb 3, 2022
This repo contain builders of cab file, html file, and docx file for CVE-2021-40444 exploit

CVE-2021-40444 builders This repo contain builders of cab file, html file, and docx file for CVE-2021-40444 exploit. This repo is just for testing, re

ASL IT Security 162 Jan 30, 2022
Parser manager for parsing DOC, DOCX, PDF or HTML files

Parser manager Description Parser gets PDF, DOC, DOCX or HTML file via API and saves parsed data to the database. Implemented in Ruby 3.0.1 using Acti

Эдем 4 Dec 4, 2021
This is a file deletion program that asks you for an extension of a file (.mp3, .pdf, .docx, etc.) to delete all of the files in a dir that have that extension.

FileBulk This is a file deletion program that asks you for an extension of a file (.mp3, .pdf, .docx, etc.) to delete all of the files in a dir that h

Enoc Mena 1 Nov 8, 2021
Simple python tool for the purpose of swapping latinic letters with cirilic ones and vice versa in txt, docx and pdf files in Serbian language

Alpha Swap English This is a simple python tool for the purpose of swapping latinic letters with cirylic ones and vice versa, in txt, docx and pdf fil

Aleksandar Damnjanovic 2 Dec 30, 2021
The best way to convert files on your computer, be it .pdf to .png, .pdf to .docx, .png to .ico, or anything you can imagine.

The best way to convert files on your computer, be it .pdf to .png, .pdf to .docx, .png to .ico, or anything you can imagine.

JareBear 2 Nov 20, 2021
Генератор отчетов на Python с использованием библиотеки docx для работы с word-файлами и запросов к сервису

Генератор отчетов на Python с использованием библиотеки docx для работы с word-файлами и запросов к сервису

Semyon Esaev 3 Dec 20, 2021
Flask Project Template A full feature Flask project template.

Flask Project Template A full feature Flask project template. See also Python-Project-Template for a lean, low dependency Python app. HOW TO USE THIS

Bruno Rocha 41 Feb 13, 2022
Template for a Dataflow Flex Template in Python

Dataflow Flex Template in Python This repository contains a template for a Dataflow Flex Template written in Python that can easily be used to build D

STOIX 5 Feb 2, 2022
This project uses Template Matching technique for object detecting by detection of template image over base image.

Object Detection Project Using OpenCV This project uses Template Matching technique for object detecting by detection the template image over base ima

Pratham Bhatnagar 5 Dec 10, 2021
This project uses Template Matching technique for object detecting by detection of template image over base image

Object Detection Project Using OpenCV This project uses Template Matching technique for object detecting by detection the template image over base ima

Pratham Bhatnagar 4 Nov 16, 2021
Discord.py-Bot-Template - Discord Bot Template with Python 3.x

Discord Bot Template with Python 3.x This is a template for creating a custom Di

Keagan Landfried 3 Feb 5, 2022
Streamlit-template - A streamlit app template based on streamlit-option-menu

streamlit-template A streamlit app template for geospatial applications based on

Qiusheng Wu 18 Feb 13, 2022
Pincer-bot-template - A template for a Discord bot created using the Pincer library

Pincer Discord Bot Template (Python) WARNING: Pincer is still in its alpha/plann

binds 2 Jan 6, 2022
Fastapi-ml-template - Fastapi ml template with python

FastAPI ML Template Run Web API Local $ sh run.sh # poetry run uvicorn app.mai

Yuki Okuda 5 Feb 15, 2022
A flask template with Bootstrap 4, asset bundling+minification with webpack, starter templates, and registration/authentication. For use with cookiecutter.

cookiecutter-flask A Flask template for cookiecutter. (Supports Python ≥ 3.6) See this repo for an example project generated from the most recent vers

null 4k Feb 14, 2022
HTML Template Linter and Formatter. Use with Django, Jinja, Nunjucks and Handlebars templates.

Find common formatting issues and reformat HTML templates. Django · Jinja · Nunjucks · Handlebars · Mustache · GoLang Ps, --check it out on other temp

Riverside Healthcare Analytics 45 Feb 7, 2022
A template that everyone can use for the start of their discord bot

Python Discord Bot Template This repository is a template that everyone can use for the start of their discord bot. When I first started creating my d

null 2 Nov 1, 2021
A template repo for use in the Advent of Code

AoC-Template A template repo for use in the Advent of Code The README_template.md must contain "STATS_TABLE" to be replaced by the generated table, an

null 0 Jan 14, 2022
Use Convolutional Recurrent Neural Network to recognize the Handwritten line text image without pre segmentation into words or characters. Use CTC loss Function to train.

Handwritten Line Text Recognition using Deep Learning with Tensorflow Description Use Convolutional Recurrent Neural Network to recognize the Handwrit

sushant097 178 Feb 10, 2022
Vulnerability Scanner & Auto Exploiter You can use this tool to check the security by finding the vulnerability in your website or you can use this tool to Get Shells

About create a target list or select one target, scans then exploits, done! Vulnnr is a Vulnerability Scanner & Auto Exploiter You can use this tool t

Nano 108 Dec 4, 2021
🤗 The largest hub of ready-to-use NLP datasets for ML models with fast, easy-to-use and efficient data manipulation tools

?? The largest hub of ready-to-use NLP datasets for ML models with fast, easy-to-use and efficient data manipulation tools

Hugging Face 12.1k Feb 11, 2022
this is a lite easy to use virtual keyboard project for anyone to use

virtual_Keyboard this is a lite easy to use virtual keyboard project for anyone to use motivation I made this for this year's recruitment for RobEn AA

Mohamed Emad 3 Oct 23, 2021