问题
I think this question is pretty self explanatory. From what I've read of the python-docx documentation, it seems that the header and footer must be exactly the same on every page, which of course makes adding page numbers difficult. Is this possible?
回答1:
Adding headers and footers is a feature not yet implemented.
However...
If it is an existing document you want to add headers and footers to you can call a VBA-macro. I recently posted a way to do that (https://stackoverflow.com/a/44767400/7386332)
If it is a new document then you can indeed go on and create a template document first and then open it up and continue editing as described by scanny.
回答2:
A Word document produced by python-docx
will preserve any headers and footers present in the "template" document. So the way to get those is to create a "starting" document that has the headers and footers you're after.
A header or footer can contain a page number field, which is automatically updated with the current page number at display and/or print time. This is added using the Insert > Field > Page Number menu option in Word. Different Word versions do this slightly differently, but this should get you close enough to find it on your version. Otherwise a search on "Insert page number Word 2013" with your version will find you many resources.
来源:https://stackoverflow.com/questions/44788862/how-can-i-add-page-numbers-to-each-pages-footer-with-python-docx