Search code examples
pythonpython-2.7docxpython-docx

Python docx not finding all sections


I need this code to be able to work with existing documents I have, but it is only finding one footer in each document. I need it to find all 3 (first page, even, and odd). Here is the link to my test document: http://www.filedropper.com/testdoc_1

from docx import Document
document = Document('C:/Users/username/Desktop/SPECS/TEST DOC.docx')
print(len(document.sections))

for section in document.sections:
    footer = section.footer
    footer.paragraphs[0].text  = footer.paragraphs[0].text.replace("ISSUED FOR CONSTRUCTION", "DESIGN DEVELOPMENT")


document.save('C:/Users/username/Desktop/SPECS/TEST DOC.docx') 

Solution

  • So I answered my question by reading this documentation: https://python-docx.readthedocs.io/en/latest/api/section.html

    The default is the odd page footer. To access the other footers (if you enabled different first page and even footers) you have to use the below code:

    first_page_footer = section.first_page_footer
    even_page_footer = section.even_page_footer
    odd_page_footer = section.footer