Zoned Footer
To add a zoned footer we will use tabs i.e ‘\t‘. There are three zones left, Centre and right. The text is by default in zone left, if we use single ‘\t‘ on the text then it will shift to the center zone and with one more ‘\t‘ to the right zone respectively.
Example 2: Adding a left zoned 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 the left zoned footer footer_para.text = "This is the left zoned footer..." # Add a Title to the document doc.add_heading( 'w3wiki' , 0 ) # Now save the document to a location doc.save( 'gfg.docx' ) |
Output:
Example 3: Adding a center zoned 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 the centered zoned footer footer_para.text = "\tThis is the centered zoned footer..." # Add a Title to the document doc.add_heading( 'w3wiki' , 0 ) # Now save the document to a location doc.save( 'gfg.docx' ) |
Output:
Example 4: Adding a right zoned 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 the right zoned footer footer_para.text = "\t\tThis is the right zoned 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