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( 'w3wiki' , 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( 'w3wiki' , 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( '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