Search code examples
pythonpython-docx

Attribute that defines a paragraph is a bulleted list style in python-docx


I'm working with a document that has a custom Style that has a custom bulleted list style applied to it. I need to be able to inspect this style and understand that it is a bulleted list. Is there a property of the style that would be associated with this? I've been looking at the the properties within the paragraph format like so:

document = Document('example.docx')
styles = document.styles
for style in style:
    print(dir(style.paragraph_format))

but none of these objects that I've looked at thus far make it clear that there's a bulleted list style. Is this information stored somewhere? As I said, the name of the style is custom and so isn't named as some built-in List Bullet style or anything like that.


Solution

  • There is currently no API support in python-docx for detecting this condition. You would need to inspect the XML of the style and you may need to walk through a few references as there is potentially some inheritance involved.

    I'd recommend starting with a paragraph style you know has bullets applied and print(style._element.xml) and see what you can make out by way of an element is "number" in its name or whatever hints you can find.

    I will tell you that list formatting is a complex matter in Word. You can probably find some other resources on search that may give clues, this one came up on a quick searh: Bullet Lists in python-docx