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

强颜欢笑 提交于 2019-12-08 03:09:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!