Simple Footer
A footer object is always present at the bottom of the section or page and can be called by the use of section.footer. Each new footer contains an empty paragraph and it can be edited like the rest of the document. To add content we make use of the .text method of the paragraph.
Example 1: Adding a footer to a Word document.
Python3
# Import docx NOT python-docx import docx # Create an instance of a word document doc = docx.Document() # Choosing the top most section of the page section = doc.sections[ 0 ] # Calling the footer footer = section.footer # Calling the paragraph already present in # the footer section footer_para = footer.paragraphs[ 0 ] # Adding text in the footer footer_para.text = "This is a footer..." # Add a Title to the document doc.add_heading( 'w3wiki' , 0 ) # Now save the document to a location doc.save( 'gfg.docx' ) |
Output:
Working with Headers And Footers in Python .docx Module
Prerequisite: Working with .docx module
Word documents contain formatted text wrapped within three object levels. The Lowest level-run objects, middle level-paragraph objects, and highest level-document objects. So, we cannot work with these documents using normal text editors. But, we can manipulate these word documents in python using the python-docx module. Pip command to install this module is:
pip install python-docx
Python docx module allows users to manipulate docs by either manipulating the existing one or creating a new empty document and manipulating it. It is a powerful tool as it helps you to manipulate the document to a very large extend. Both header and footer are a part of a section so that each section can have a different header and footer. The header is an important part of the document as it contains important information regarding the document which the publisher wants to display on each page.
Contact Us