The Wayback Machine - https://web.archive.org/web/20240717024729/https://www.geeksforgeeks.org/working-with-page-break-python-docx-module/
Open In App

Working with Page Break – Python .docx Module

Last Updated : 03 Jan, 2021
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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 in python using the python-docx module. 

Python docx module allows user 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. You can also add page breaks using this module. To add a page break in a word document you can use add_page_break() method. 

This method adds a hard page break in the document and creates a new paragraph object. This is an essential paragraph styling tool. Whenever the requirement is to start a new page, mostly cases where you want a new page for a new topic, a single method is sufficient. It helps increase clarity and improves presentation to reap the most of word.

Syntax: doc.add_page_break()

Approach

  • Import module
  • Create docx object
  • Add add_page_break() function whenever the control need to be shift to a new page
  • Save document.

Example:

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 a paragraph
doc.add_heading('Page 1:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
  
# Adding a page break
doc.add_page_break()
  
# Adding a paragraph
doc.add_heading('Page 2:', 3)
doc.add_paragraph('GeeksforGeeks is a Computer Science portal for geeks.')
  
# Now save the document to a location
doc.save('gfg.docx')


Output:

Page – 1

Page – 2



Similar Reads

Working with Page Orientations and Pagination Properties - 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 manipulate these word documents in python using the pytho
4 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 manipulate these word documents in python using the pytho
4 min read
Working With Text 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 object. So, we cannot work with these documents using normal text editors. But, we can manipulate these word documents in python using the python-d
8 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 manipulate these word documents in python using the python-
6 min read
Working with Documents - 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 manipulate these word documents in python using the pytho
2 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 documents in python using the python-docx module. Python
2 min read
Working with Images - 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 in python using the python-docx module. Python doc
2 min read
Working with Tables - 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 in python using the python-docx module. Python doc
3 min read
Working with Paragraphs in 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 documents in python using the python-docx module. Pip co
4 min read
Working with Highlighted Text in 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 documents in python using the python-docx module. Pip co
5 min read
Practice Tags :
three90RightbarBannerImg