Skip to content

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

@micabot

Description

@micabot

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

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy