Add content to existing docx using python-docx
I would like to open an existing word document where I have already added page numbers and just add text and title to it.
Here is a basic example of how I tried to achieve my goal
#!/usr/bin/env python
from docx import Document
document = Document('report-template.docx')
document.add_heading('Headline No. 1', level=1)
document.add_paragraph('Test No. 1')
document.add_heading('Heading No. 2', level=2)
document.add_paragraph('Test No. 2')
document.save('example.docx')
When I do all of the above with a complete new document everything works fine - when doing this with an already existing file, it fails with the following error
Traceback (most recent call last):
File "create-report-test.py", line 6, in <module>
document.add_heading('Headline No. 1', level=1)
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 1'"
I have read the doc under http://python-docx.readthedocs.org/en/latest/user/documents.html but it seems like I am missing something - does anyone have an idea?
Thank you in advance
source to share
python-docx
can only work with styles that are already defined in the document. This error indicates that the heading 1 paragraph style is undefined. Word starts with no specific styles (ok, a couple like Normal, but that's it), then adds inline styles to the file on first use.
Two options:
-
Add Heading 1 to your document manually, and then remove it. The document then defines the heading paragraph 1 style. When Word adds a style, it does not remove it, even if all paragraphs using that style are removed.
-
Use python-docx to define header 1 yourself. See the documentation here on how to do this: http://python-docx.readthedocs.org/en/latest/user/styles-using.html#define-paragraph-formatting
This page is probably also worth a quick read to fill in some style concepts: http://python-docx.readthedocs.org/en/latest/user/styles-understanding.html
source to share