← 返回首页
docs: update styles to indicate exception raised on style not defined · Issue #170 · python-openxml/python-docx · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

docs: update styles to indicate exception raised on style not defined #170

New issue
New issue

Description

Whenever I add a custom style to the template default.docx and try to create a document I get the following error:

Traceback (most recent call last): File "helloworld.py", line 9, in <module> document.add_heading('smiling having fun', 2) File "/usr/lib/python2.7/site-packages/docx/document.py", line 43, in add_heading return self.add_paragraph(text, style) File "/usr/lib/python2.7/site-packages/docx/document.py", line 63, in add_paragraph return self._body.add_paragraph(text, style) File "/usr/lib/python2.7/site-packages/docx/blkcntnr.py", line 38, in add_paragraph paragraph.style = style File "/usr/lib/python2.7/site-packages/docx/text/paragraph.py", line 111, in style style_or_name, WD_STYLE_TYPE.PARAGRAPH File "/usr/lib/python2.7/site-packages/docx/parts/document.py", line 75, in get_style_id return self.styles.get_style_id(style_or_name, style_type) File "/usr/lib/python2.7/site-packages/docx/styles/styles.py", line 113, in get_style_id return self._get_style_id_from_name(style_or_name, style_type) File "/usr/lib/python2.7/site-packages/docx/styles/styles.py", line 143, in _get_style_id_from_name return self._get_style_id_from_style(self[style_name], style_type) File "/usr/lib/python2.7/site-packages/docx/styles/styles.py", line 57, in __getitem__ raise KeyError("no style with name '%s'" % key) KeyError: u"no style with name 'Heading 2'"

This happens with python 2.7 using python-docx 0.8.5, and it didn't happend with 0.7.4.
The code I'm using to generate the document is:

#!/usr/bin/env python2.7 import os import os.path from docx import Document PATH = os.path.dirname(os.path.realpath(__file__)) + '/' document = Document(os.path.join(PATH, 'default.docx')) document.add_heading('smiling having fun', 2) document.add_paragraph('hello world?') document.save('helloworld.docx')

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Footer

      © 2026 GitHub, Inc.