Working with Headers And Footers in Python .docx Module

08 Mar, 2022
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.
A header object is always present on the top of the section or page and can be called by the use of section.header. Each new header contains an empty paragraph and it can be edited like the rest of the document. To add content we use the .text method of the paragraph.
Example 1: Adding a header in the Word document.
Python3
# Import docx NOT python-docximport docx # Create an instance of a word documentdoc = docx.Document() # Choosing the top most section of the pagesection = doc.sections[0] # Calling the headerheader = section.header # Calling the paragraph already present in# the header sectionheader_para = header.paragraphs[0] # Adding text in the headerheader_para.text = "This is a header..." # Add a Title to the documentdoc.add_heading('GeeksForGeeks', 0) # Now save the document to a locationdoc.save('gfg.docx')
  Output:
By using this module you can also add a zoned header in the Word document. To add a zoned header we use tabs i.e ‘\t‘. There are three zones left, Centre and right. The text is by default in zone left, if we use a 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 header to a Word document.
Python3
# Import docx NOT python-docximport docx # Create an instance of a word documentdoc = docx.Document() # Choosing the top most section of the pagesection = doc.sections[0] # Selecting the headerheader = section.header # Selecting the paragraph already present in# the header sectionheader_para = header.paragraphs[0] # Adding the left zoned headerheader_para.text = "This is Left Zoned Header..." # Add a Title to the documentdoc.add_heading('GeeksForGeeks', 0) # Now save the document to a locationdoc.save('gfg.docx')
Output:
 
Example 3: Adding a centered zoned header to a Word document.
Python3
# Import docx NOT python-docximport docx # Create an instance of a word documentdoc = docx.Document() # Choosing the top most section of the pagesection = doc.sections[0] # Selecting the headerheader = section.header # Selecting the paragraph already present in# the header sectionheader_para = header.paragraphs[0] # Adding the centred zoned headerheader_para.text = "\tThis is Centred Zoned Header..." # Add a Title to the documentdoc.add_heading('GeeksForGeeks', 0) # Now save the document to a locationdoc.save('gfg.docx')
 
Output:
Example 4: Adding a right zoned header to a Word document.
 
Python3
# Import docx NOT python-docximport docx # Create an instance of a word documentdoc = docx.Document() # Choosing the top most section of the pagesection = doc.sections[0] # Selecting the headerheader = section.header # Selecting the paragraph already present in# the header sectionheader_para = header.paragraphs[0] # Adding the right zoned headerheader_para.text = "\t\tThis is Right Zoned Header..." # Add a Title to the documentdoc.add_heading('GeeksForGeeks', 0) # Now save the document to a locationdoc.save('gfg.docx')
Output:
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-docximport docx # Create an instance of a word documentdoc = docx.Document() # Choosing the top most section of the pagesection = doc.sections[0] # Calling the footerfooter = section.footer # Calling the paragraph already present in# the footer sectionfooter_para = footer.paragraphs[0] # Adding text in the footerfooter_para.text = "This is a footer..." # Add a Title to the documentdoc.add_heading('GeeksForGeeks', 0) # Now save the document to a locationdoc.save('gfg.docx')
Output:
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-docximport docx # Create an instance of a word documentdoc = docx.Document() # Choosing the top most section of the pagesection = doc.sections[0] # Calling the footerfooter = section.footer # Calling the paragraph already present in# the footer sectionfooter_para = footer.paragraphs[0] # Adding the left zoned footerfooter_para.text = "This is the left zoned footer..." # Add a Title to the documentdoc.add_heading('GeeksForGeeks', 0) # Now save the document to a locationdoc.save('gfg.docx')
  
Output:
 
 
 
Example 3: Adding a center zoned footer to a Word document.
 
Python3
# Import docx NOT python-docximport docx # Create an instance of a word documentdoc = docx.Document() # Choosing the top most section of the pagesection = doc.sections[0] # Calling the footerfooter = section.footer # Calling the paragraph already present in# the footer sectionfooter_para = footer.paragraphs[0] # Adding the centered zoned footerfooter_para.text = "\tThis is the centered zoned footer..." # Add a Title to the documentdoc.add_heading('GeeksForGeeks', 0) # Now save the document to a locationdoc.save('gfg.docx')
  
Output:
 
Example 4: Adding a right zoned footer to a Word document.
 
Python3
# Import docx NOT python-docximport docx # Create an instance of a word documentdoc = docx.Document() # Choosing the top most section of the pagesection = doc.sections[0] # Calling the footerfooter = section.footer # Calling the paragraph already present in# the footer sectionfooter_para = footer.paragraphs[0] # Adding the right zoned footerfooter_para.text = "\t\tThis is the right zoned footer..." # Add a Title to the documentdoc.add_heading('GeeksForGeeks', 0) # Now save the document to a locationdoc.save('gfg.docx')
  
Output:
 
 
anikakapoor
sagar0719kumar
kothavvsaakash
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Convert integer to string in Python
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe