Skip to content

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

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 (mostrecentcalllast):
File"helloworld.py", line9, in<module>document.add_heading('smiling having fun', 2)
File"/usr/lib/python2.7/site-packages/docx/document.py", line43, inadd_headingreturnself.add_paragraph(text, style)
File"/usr/lib/python2.7/site-packages/docx/document.py", line63, inadd_paragraphreturnself._body.add_paragraph(text, style)
File"/usr/lib/python2.7/site-packages/docx/blkcntnr.py", line38, inadd_paragraphparagraph.style=styleFile"/usr/lib/python2.7/site-packages/docx/text/paragraph.py", line111, instylestyle_or_name, WD_STYLE_TYPE.PARAGRAPHFile"/usr/lib/python2.7/site-packages/docx/parts/document.py", line75, inget_style_idreturnself.styles.get_style_id(style_or_name, style_type)
File"/usr/lib/python2.7/site-packages/docx/styles/styles.py", line113, inget_style_idreturnself._get_style_id_from_name(style_or_name, style_type)
File"/usr/lib/python2.7/site-packages/docx/styles/styles.py", line143, in_get_style_id_from_namereturnself._get_style_id_from_style(self[style_name], style_type)
File"/usr/lib/python2.7/site-packages/docx/styles/styles.py", line57, in__getitem__raiseKeyError("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.7importosimportos.pathfromdocximportDocumentPATH=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