Working with Page Orientations and Pagination Properties - Python .docx Module
Last Updated :
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.
Page Orientations
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-docx
import docx
# Create an instance of a word document
doc = docx.Document()
# Selecting a section of the document
section = 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-docx
import docx
from docx.enum.section import WD_ORIENT
# Create an instance of a word document
doc = docx.Document()
# Selecting a section of the document
section = doc.sections[0]
# Printing the default orientation.
print("Default Orientation:",
section.orientation)
# Changing the orientation to landscape
section.orientation = WD_ORIENT.LANDSCAPE
# Printing the new orientation.
print("New Orientation:",
section.orientation)
Â
Â
Output:
Â
Default Orientation: PORTRAIT (0)
New Orientation: LANDSCAPE (1)
Pagination Properties
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-docx
import docx
# Create an instance of a word document
doc = docx.Document()
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
# Adding paragraph
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
# Setting keep_together as True
para.keep_together = True
# Now save the document to a location
doc.save('gfg.docx')
Â
Â
Output:
Â

Â
Â
Example 4: Using keep_with_next on a paragraph in a word document.
Â
Python3
# Import docx NOT python-docx
import docx
# Create an instance of a word document
doc = docx.Document()
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
# Adding paragraph
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
# Setting keep_with_next as True
para.keep_with_next = True
# Now save the document to a location
doc.save('gfg.docx')
Â
Â
Output:
Â

Â
Â
Example 5: Using page_break_before on a paragraph in a word document.
Â
Python3
# Import docx NOT python-docx
import docx
# Create an instance of a word document
doc = docx.Document()
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
# Adding paragraph
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
# Setting page_break_before as True
para.page_break_before = True
# Now save the document to a location
doc.save('gfg.docx')
Â
Â
Output:
Â

Example 6: Using widow_control on a paragraph in a Word document.
Â
Python3
# Import docx NOT python-docx
import docx
# Create an instance of a word document
doc = docx.Document()
# Add a Title to the document
doc.add_heading('GeeksForGeeks', 0)
# Adding paragraph
para = doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
# Setting widow_control as True
para.widow_control = True
# Now save the document to a location
doc.save('gfg.docx')
Â
Â
Output:
Â
Â
Similar Reads
Working with Page Break - Python .docx Module Prerequisites: docx Word documents contain formatted text wrapped within three object levels. 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
2 min read
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 manipulat
6 min read
Working with Titles and Heading - Python docx Module Prerequisites: docx 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 docume
2 min read
Working with Lists - 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 object. So, we cannot work with these documents using normal text editors. But, we can manipul
4 min read
Pagination - xpath for a crawler in Python In this article, we are going to learn about pagination using XPath for a crawler in Python. This article is about learning how to extract information from different websites where information is stored on multiple pages. So to move to all pages via API's call we use a concept of paging which helps
6 min read
How To Do Pagination In Python In this article, we'll walk you through the steps of setting up pagination in Python. We'll explain each step clearly and straightforwardly. To help you understand the idea of pagination in Python, we'll show you how to create a pagination system using the Tkinter library. We'll start by explaining
5 min read
How to add Pagination in Django Project? Pagination system is one of the most common features in  blogs, search engine , list of result etc. Seeing the popularity of pagination system django developers have build a Paginator class so that web developers do not have to think of the logic to make paginators. What is the Paginator Class?The P
4 min read
Angular PrimeNG Paginator Rows Per Page Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will learn how to use the Paginator Rows Per Page in Angular PrimeNG. We will al
3 min read
How to count the number of pages in a PDF file in Python In this article, we will see how can we count the total number of pages in a PDF file in Python, For this article there is no such prerequisite, we will use PyPDF2 library for this purpose. PyPDF2 is a free and open-source pure-Python PyPDF library capable of performing many tasks like splitting, me
4 min read