Skip to content

Why is my own defined style persistent? #1346

@buhtz

Description

@buhtz

I do add a new style to a document and then use its name when adding paragraphs for example. The resulting docx file looks as expected. But I was supprised to realize that this style then is persistent. I ran another script without defining that style but accidentally discovered the style (by name) is available in the new document instance.

Might it be that python-docx use a template file in the back and store new style into that template file, too?

The behavior is unexpected. Not sure if it make sense to modify that behavior.
But beside of that question I would like to know if there is a way to "reset" the template file to its original state?

This is how I create and add such a style.

from docx.enum.style import WD_STYLE_TYPE
from docx.shared import Pt
style = doc._doc.styles.add_style('Foobar', WD_STYLE_TYPE.PARAGRAPH)
style.base_style = doc._doc.styles['Normal']
style.font.name = 'Cascadia Mono'
style.font.size = Pt(10)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions