python-docx: Using "list Bullet" in newly created .docx

Viewed 108

I have a procedure that reads in data from an excel file and after some processing creates a new word document and writes that data in there.

In the beginning of the document there need to be some paragraphs with bullet points, however when using

import docx
Bullet1 = "This text is for Bullet 1."
doc.add_paragraph(Bullet1, style='List Bullet')

I get the error

KeyError: "no style with name 'List Bullet'"

I looked at which styles are available, and found that there are only four of them. So I did some reading and found out, that the style needs to be used at least once in the document, for it to be accessable. If I understand that right, this means that I have to create a document by hand, use bullet points in there, delete them again, and then the style "list bullet" should be available.

However the goal is that this whole process of generating a new document and filling it with data is supposed to be fully automatic, therefore creating a word document by hand, using bullet points and then deleting the content again, is not an option.

How do I deal with this issue?

1 Answers
import docx
doc = docx.Document()
Bullet1 = "This text is for Bullet 1."
doc.add_paragraph(Bullet1, style='List Bullet')
doc.save("test.docx")

For me (using docx 0.8.11) this works as expected and doesn't raise an error.

Related