pythonpython-docx

How to use python-docx-template to insert bullet points?


I'm trying to insert bullet point text with docx-template. I know that it can be done with the standard docx like below;

document.add_paragraph('text to be bulleted', style='List Bullet')

But I just can't get the same thing to work on docx-template;

rt = RichText()
rt.add('text to be bulleted', style='List Bullet')

The above code just returns 'text to be bulleted' without the bullet in front.

I've looked through all the "tests" on the github and nothing mentions bullet. Any assistance would be greatly appreciated. Thank you in advance.


Solution

  • Until this is officially supported (open issue), you might use a workaround (but only fixed indentations):

    In your word document add this (with a real single bullet list item):

    {% for bullet in bullets %}
      ● {{ bullet }}{% endfor %}
    

    Note that the {% endfor %} must be in the same line to avoid blank lines between the bullet items.

    Use this python code here:

    from docxtpl import DocxTemplate
    
    tpl=DocxTemplate('template.docx')
    
    context = {
        'bullets': [
            'item 1',
            'item 2',
        ],
    }
    
    tpl.render(context)
    tpl.save("output.docx")