Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WD_BUILTIN_STYLE gives no style with name #1439

Open
cmhughes opened this issue Oct 16, 2024 · 1 comment
Open

WD_BUILTIN_STYLE gives no style with name #1439

cmhughes opened this issue Oct 16, 2024 · 1 comment

Comments

@cmhughes
Copy link

Starting from the minimal code given at https://python-docx.readthedocs.io/en/stable/api/enum/WdBuiltinStyle.html

from docx import Document
from docx.enum.style import WD_STYLE

document = Document()
styles = document.styles
style = styles[WD_STYLE.BODY_TEXT]

gives the following error:

KeyError: "no style with name 'BODY_TEXT (-67)'"

I'm sure I'm doing something wrong, but if you know of a way I can fix this, I'd be very grateful. Thank you for your time!

@scanny
Copy link
Contributor

scanny commented Oct 16, 2024

Not all documents will have all styles defined. A good next step might be to list out the styles in the current document with something like:

for style in document.styles:
    print(style.name)

It could also be that documentation is out of date, you might try styles["Body Text"] or styles["BodyText"]. I believe we changed from looking up by WD_STYLE member to style.name a while back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants