Search code examples
pythonpython-2.7python-3.xscriptingpython-docx

Python-docx issue with HTML preformatted text


I'm trying to implement a script that pulls in HTML, and converts it to a formatted Word document. I'm having particular trouble with one style from the built-in styles defined in docx.enum.style

I found this page that describes a code snippet to use the WD_STYLE.HTML_PRE attribute to assign a style (http://python-docx.readthedocs.org/en/latest/api/enum/WdBuiltinStyle.html#wd-builtin-style)

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

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

When I try this set of commands as described in the URL above, I get the following error:

>>> from docx import Document
>>> from docx.enum.style import WD_STYLE
>>>
>>> document = Document()
>>> styles = document.styles
>>> style = styles[WD_STYLE.BODY_TEXT]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\docx\styles\styles.py", line 57, in __getitem__
    raise KeyError("no style with name '%s'" % key)
KeyError: u"no style with name 'BODY_TEXT (-67)'"
>>> style = styles[WD_STYLE.HTML_PRE]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\docx\styles\styles.py", line 57, in __getitem__
    raise KeyError("no style with name '%s'" % key)
**KeyError: u"no style with name 'HTML_PRE (-102)'"**

What am I doing wrong?


Solution

  • You should read these two pages in the documentation:

    The functionality in the example given as part of the WD_BUILTIN_STYLE enumeration was not implemented, so that approach won't work.

    Use the name of the style you want, as it appears in the Word UI:

    style = styles["Body Text"]
    

    After reading the documentation pages above you'll understand how this won't work unless the style you asked for is explicitly defined in the document, and it will explain how to manage that.