4

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.

Nerman
  • 53
  • 5
  • Does this answer your question? [Bullet Lists in python-docx](https://stackoverflow.com/questions/51829366/bullet-lists-in-python-docx) - Edit : the question I linked concerns the `document.add_paragraph` function. – Frodon Apr 12 '22 at 00:13

1 Answers1

4

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")
Felix
  • 356
  • 4
  • 7