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.
Simple Header
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-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 header header = section.header # Calling the paragraph already present in # the header section header_para = header.paragraphs[ 0 ] # Adding text in the header header_para.text = "This is a header..." # Add a Title to the document doc.add_heading( 'GeeksForGeeks' , 0 ) # Now save the document to a location doc.save( 'gfg.docx' ) |
Output:
Zoned Header
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-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 ] # Selecting the header header = section.header # Selecting the paragraph already present in # the header section header_para = header.paragraphs[ 0 ] # Adding the left zoned header header_para.text = "This is Left Zoned Header..." # Add a Title to the document doc.add_heading( 'GeeksForGeeks' , 0 ) # Now save the document to a location doc.save( 'gfg.docx' ) |
Output:
Example 3: Adding a centered zoned header 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 ] # Selecting the header header = section.header # Selecting the paragraph already present in # the header section header_para = header.paragraphs[ 0 ] # Adding the centred zoned header header_para.text = "\tThis is Centred Zoned Header..." # Add a Title to the document doc.add_heading( 'GeeksForGeeks' , 0 ) # Now save the document to a location doc.save( 'gfg.docx' ) |
Output:
Example 4: Adding a right zoned header 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 ] # Selecting the header header = section.header # Selecting the paragraph already present in # the header section header_para = header.paragraphs[ 0 ] # Adding the right zoned header header_para.text = "\t\tThis is Right Zoned Header..." # Add a Title to the document doc.add_heading( 'GeeksForGeeks' , 0 ) # Now save the document to a location doc.save( 'gfg.docx' ) |
Output:
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( 'GeeksForGeeks' , 0 ) # Now save the document to a location doc.save( 'gfg.docx' ) |
Output:
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( 'GeeksForGeeks' , 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( 'GeeksForGeeks' , 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( 'GeeksForGeeks' , 0 ) # Now save the document to a location doc.save( 'gfg.docx' ) |
Output: