Working with Page Orientations and Pagination Properties – Python .docx Module

09 Jul, 2021
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 object. 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.
To change the orientation of the word document we make use of the WD_ORIENT of the docx.enum.section module. And to call or set the orientation of the section we will use the orientation method of the section class.
Syntax:
section.orientation = WD_ORIENT.[Orientation Type]
There are two types of orientations possible.
Sr. No.
Orientation Type
Description
1.
Portrait
It is used to set the orientation to portrait.
2.
Landscape
It is used to set the orientation to landscape.
Note:
The portrait is the default orientation.
Orientation methods can only be used upon sections so to use one you have to first select a section of the Word document.
Example 1: Printing the default orientation of the Word document.
Python3
# Import docx NOT python-docximport docx # Create an instance of a word documentdoc = docx.Document() # Selecting a section of the documentsection = doc.sections[0] # Printing the default orientation.print("Default Orientation:", section.orientation)
  
Output:
 
Default Orientation: PORTRAIT (0)
 
 
Example 2: Changing the orientation to the landscape from default.
 
Python3
# Import docx NOT python-docximport docxfrom docx.enum.section import WD_ORIENT # Create an instance of a word documentdoc = docx.Document() # Selecting a section of the documentsection = doc.sections[0] # Printing the default orientation.print("Default Orientation:",      section.orientation) # Changing the orientation to landscapesection.orientation = WD_ORIENT.LANDSCAPE # Printing the new orientation.print("New Orientation:",      section.orientation)
  
Output:
 
Default Orientation: PORTRAIT (0)
New Orientation: LANDSCAPE (1)
Pagination properties are the properties or styles which control the behavior of the paragraphs near the page boundaries. There are four styles of add_paragraph() function in .docx module which comes under this category.
 
Syntax:
 
doc.add_paragraph(String s, style=None)
Parameters:
 
String s: It is the string data that is to be added as a paragraph. This string can contain newline character ‘\n‘, tabs ‘\t‘ or a carriage return character ‘\r‘.
Style: It is used to set style.
Styles that come under pagination properties are:
 
Sr. NO.
Style Name
Description
1.
keep_together
Keeps the content of the paragraph on one page.
2.
keep_with_next
Keeps the content of a paragraph with another subsequent paragraph.
3.
page_break_before
Move a paragraph to a new page because of a page break.
4.
widow_control
Keeps the first and last line of the paragraph together with the rest of the paragraph. 
Note: All the four styles can either be set to true, false, or none. True means “on”, False means “off” and None means the property is inherited from the style hierarchy.
 
Example 3: Using keep_together on a paragraph in a Word document.
 
Python3
# Import docx NOT python-docximport docx # Create an instance of a word documentdoc = docx.Document() # Add a Title to the documentdoc.add_heading('GeeksForGeeks', 0) # Adding paragraphpara = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.') # Setting keep_together as Truepara.keep_together = True # Now save the document to a locationdoc.save('gfg.docx')
  
Output:
 
 
 
Example 4: Using keep_with_next on a paragraph in a word document.
 
Python3
# Import docx NOT python-docximport docx # Create an instance of a word documentdoc = docx.Document() # Add a Title to the documentdoc.add_heading('GeeksForGeeks', 0) # Adding paragraphpara = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.') # Setting keep_with_next as Truepara.keep_with_next = True # Now save the document to a locationdoc.save('gfg.docx')
  
Output:
 
 
 
Example 5: Using page_break_before on a paragraph in a word document.
 
Python3
# Import docx NOT python-docximport docx # Create an instance of a word documentdoc = docx.Document() # Add a Title to the documentdoc.add_heading('GeeksForGeeks', 0) # Adding paragraphpara = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.') # Setting page_break_before as Truepara.page_break_before = True # Now save the document to a locationdoc.save('gfg.docx')
  
Output:
 
Example 6: Using widow_control on a paragraph in a Word document.
 
Python3
# Import docx NOT python-docximport docx # Create an instance of a word documentdoc = docx.Document() # Add a Title to the documentdoc.add_heading('GeeksForGeeks', 0) # Adding paragraphpara = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.') # Setting widow_control as Truepara.widow_control = True # Now save the document to a locationdoc.save('gfg.docx')
  
Output:
 
 
surinderdawra388
Python Docx-module
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.