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

Table of Contents header #542

Open
TheGroundZero opened this issue Aug 31, 2018 · 0 comments
Open

Table of Contents header #542

TheGroundZero opened this issue Aug 31, 2018 · 0 comments

Comments

@TheGroundZero
Copy link

Hi

I've added a Table of Contents (ToC) to my document via Mustash's code in #36

Now I'd like to add a header above it, in the ToC Header style (so that it doesn't add it's own header in the table).

However, I seem to be unable to find the correct style for this in the docs.

from docx.enum.style import WD_BUILTIN_STYLE

document = Document()
document.add_paragraph('Table of Contents', style=WD_BUILTIN_STYLE.INDEX_HEADING)
document.save()
Traceback (most recent call last):
  [ . . . ]
  File "/root/TOOLS/.../.../libs/export.py", line 330, in export_to_word
    document.add_paragraph('Table of Contents', style=WD_BUILTIN_STYLE.INDEX_HEADING)
  File "/usr/local/lib/python3.7/dist-packages/docx/document.py", line 63, in add_paragraph
    return self._body.add_paragraph(text, style)
  File "/usr/local/lib/python3.7/dist-packages/docx/blkcntnr.py", line 38, in add_paragraph
    paragraph.style = style
  File "/usr/local/lib/python3.7/dist-packages/docx/text/paragraph.py", line 111, in style
    style_or_name, WD_STYLE_TYPE.PARAGRAPH
  File "/usr/local/lib/python3.7/dist-packages/docx/parts/document.py", line 76, in get_style_id
    return self.styles.get_style_id(style_or_name, style_type)
  File "/usr/local/lib/python3.7/dist-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/local/lib/python3.7/dist-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/local/lib/python3.7/dist-packages/docx/styles/styles.py", line 57, in __getitem__
    raise KeyError("no style with name '%s'" % key)
KeyError: "no style with name 'INDEX_HEADING (-34)'"
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

1 participant