Is there a workaround for adding headers and footers in a Microsoft Word (docx
) file?
These are not implemented in versions of python-docx
prior to 0.8.8
.
More specifically, I would like to add:
- Page number to footers
- Some random text to headers
The ideal code will look as follows:
from docx import Document
document = Document()
# Add header and footer on all pages
document.save("demo.docx")
How about something like this (Thanks to Eliot K)
Not the most elegant approach perhaps, but should do what you need it to. Maybe sequester the ugly save/load code in a function somewhere in a dusty corner of your code ;-)
Again, does require a windows machine with microsoft office installed.
Good luck!
I've been using it to work
https://python-docx.readthedocs.io/en/latest/dev/analysis/features/header.html
It's not the most elegant (it requires you to navigate between VBA and Python), but you can use the win32com library to tap into the MS Word functionality. This of course requires a Windows machine with MS Office installed.
One of the workarounds you could use is utilizing a template document created within Word. Create a blank document, add whatever header with text you want and footer with page numbers and save the document. Then use:
... and you should be able to edit everything else while preserving the header and footer.
Ultimately I think this solution would work great for the page numbers issue. If you need unique header text for each document things could get a bit tricky. If that is the case, you might want to try editing the XML of the docx file directly. You can use this in the terminal:
... to get the docx XML files spat out into the directory. You can also use zipfile to do it within python:
The print statement will print the entire XML file, but you should be able to find this tidbit:
Which will be the text in your header. All you will have to do is edit the XML file, combine the files back together, and then change the file type back to docx.
This is obviously a super hacky workaround and unfortunately I have not been able to get it to fully work, but it at least would be a good step in the right direction if you absolutely need it.
Good luck!
The template approach works and its major advantage is that it is a truly cross-platform solution. However, it requires that a style has already been applied once in the document.
Let's consider a (simplified) version of the toy example from the
python-docx
documentation page.The first step involves creating the template document:
(Note that you can also apply the styles manually in this first step without using
python-docx
, that is from within Word.)Next, you open this
demo.docx
in Microsoft Word where you:Once you have done the above, you simply delete the main contents of the
demo.docx
document (but not the content of the header and footer!) and save the file again.In the second step, you call
demo.docx
usingpython-docx
to make the changes you need:You can even make further content additions, such as a table with an existing table style:
Of course, one can avoid repeating the first step all the time, by copying the customized file and then making the necessary changes there (e.g.
demo_copy.docx
) without affecting the template:Finally, it is worth mentioning that you can also use customized styles! For an example of how to do this using
python-docx
and table styles see here.