adamcircle
adamcircle

Reputation: 724

How can I add page numbers to each page's footer with python-docx?

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?

Upvotes: 2

Views: 4213

Answers (2)

Anton vBR
Anton vBR

Reputation: 18906

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.

Upvotes: 1

scanny
scanny

Reputation: 28883

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.

Upvotes: 0

Related Questions