Python: Insert Page Break into Word Documents

A page break is a formatting element used in documents to indicate the end of one page and the beginning of a new page. It is typically represented by a horizontal line or other visual indicator that separates content into different pages. This feature is commonly used when creating lengthy documents such as reports, essays, or books to enhance the overall layout and readability. In this article, you will learn how to how to insert page break into Word documents in Python using Spire.Doc for Python.

Install Spire.Doc for Python

This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.Doc

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows

Insert Page Break after a Specific Paragraph

Spire.Doc for Python provides Paragraph.AppendBreak(BreakType.PageBreak) method to insert a page break after a specific paragraph. The following are detailed steps.

  • Create an object of Document class.
  • Load a sample file from disk using Document.LoadFromFile() method.
  • Get the first section of this file by Document.Sections[sectionIndex] property.
  • Get the second paragraph in the section by Section.Paragraphs[paragraphIndex] property.
  • Insert a page break after this paragraph using Paragraph.AppendBreak(BreakType.PageBreak) method.
  • Save the result file using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

inputFile =  "sample.docx"
outputFile = "InsertPageBreak.docx"

#Create an object of Document class
document = Document()

#Load a sample file from disk
document.LoadFromFile(inputFile)

#Get the first section of this file
section = document.Sections[0]

#Get the second paragraph in the section
paragraph = section.Paragraphs[1]

#Insert a page break after this paragraph
paragraph.AppendBreak(BreakType.PageBreak)

#Save the result file
document.SaveToFile(outputFile, FileFormat.Docx2013)
document.Close()

Python: Insert Page Break into Word Documents

Insert Page Break after a Specific Text

What's more, you are also allowed to insert page break after a specific text by using Paragraph.ChildObjects.Insert() method provided by this library. The following are detailed steps.

  • Create an object of Document class.
  • Load a sample file from disk using Document.LoadFromFile() method.
  • Find a specific text using Document.FindAllString() method.
  • Loop through all searched text and access the text range of it by calling TextSelection.GetAsOneRange() method.
  • Get the paragraph where the text range is located by ParagraphBase.OwnerParagraph property.
  • Get the position index of the text range in the paragraph using Paragraph.ChildObjects.IndexOf() method.
  • Create an object of Break class to create a page break.
  • Insert page break after the searched text using Paragraph.ChildObjects.Insert() method.
  • Save the result file using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

inputFile = "sample.docx"
outputFile =  "InsertPageBreakAfterText.docx"

#Create an object of Document class
document = Document()

#Load a sample file from disk
document.LoadFromFile(inputFile)

#Find the specified text 
selection = document.FindAllString("fun", True, True)

#Loop through all searched text
for ts in selection:

    #Get the text range of the searched text
    range = ts.GetAsOneRange()

    #Get the paragraph where the text range is located
    paragraph = range.OwnerParagraph

    #Get the position index of the text range in the paragraph
    index = paragraph.ChildObjects.IndexOf(range)

    #Create an object of Break class  
    pageBreak = Break(document, BreakType.PageBreak)

    #Insert page break after the searched text
    paragraph.ChildObjects.Insert(index + 1, pageBreak)

#Save the result file
document.SaveToFile(outputFile, FileFormat.Docx2013)
document.Close()

Python: Insert Page Break into Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.