Python: Insert or Remove Section Breaks in Word

2024-06-12 01:10:25 Written by  support iceblue
Rate this item
(0 votes)

Section breaks in Word allow users to divide a document into sections, each with unique formatting options. This is especially useful when working with long documents where you want to apply different layouts, headers, footers, margins or page orientations within the same document. In this article, you will learn how to insert or remove section breaks in Word 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 commands.

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 Section Breaks in Word in Python

Spire.Doc for Python provides the Paragraph.InsertSectionBreak(breakType: SectionBreakType) method to insert a specified type of section break to a paragraph. The following table provides an overview of the supported section break types, along with their corresponding Enums and descriptions:

Section Break Enum Description
New page SectionBreakType.New_Page Start the new section on a new page.
Continuous SectionBreakType.No_Break Start the new section on the same page, allowing for continuous content flow.
Odd page SectionBreakType.Odd_Page Start the new section on the next odd-numbered page.
Even page SectionBreakType.Even_Page Start the new section on the next even-numbered page.
New column SectionBreakType.New_Column Start the new section in the next column if columns are enabled.

The following are the detailed steps to insert a continuous section break:

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specified section using Document.Sections[] property.
  • Get a specified paragraph of the section using Section.Paragraphs[] property.
  • Add a section break to the end of the paragraph using Paragraph.InsertSectionBreak() method.
  • Save the result document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

inputFile = "sample.docx"
outputFile = "InsertSectionBreak.docx"

# Create a Document instance
document = Document()

# Load a Word document
document.LoadFromFile(inputFile)

# Get the first section in the document
section = document.Sections[0]

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

# Insert a continuous section break
paragraph.InsertSectionBreak(SectionBreakType.NoBreak)

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

Python: Insert or Remove Section Breaks in Word

Remove Section Breaks in Word in Python

To delete all sections breaks in a Word document, we need to access the first section in the document, then copy the contents of the other sections to the first section and delete them. The following are the detailed steps:

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the first section using Document.Sections[] property.
  • Iterate through other sections in the document.
  • Get the second section, and then iterate through to get its child objects.
  • Clone the child objects of the second section and add them to the first section using Section.Body.ChildObjects.Add() method.
  • Delete the second section using Document.Sections.Remove() method.
  • Repeat the process to copy and delete the remaining sections.
  • Save the result document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

inputFile = "Report.docx"
outputFile = "RemoveSectionBreaks.docx"

# Create a Document instance
document = Document()

# Load a Word document
document.LoadFromFile(inputFile)

# Get the first section in the document
sec = document.Sections[0]

# Iterate through other sections in the document
for i in range(document.Sections.Count - 1):
    # Get the second section in the document
    section = document.Sections[1]
    
    # Iterate through all child objects of the second section
    for j in range(section.Body.ChildObjects.Count):
        # Get the child objects
        obj = section.Body.ChildObjects.get_Item(j)
        # Clone the child objects to the first section
        sec.Body.ChildObjects.Add(obj.Clone())
        # Remove the second section
        document.Sections.Remove(section)

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

Python: Insert or Remove Section Breaks in Word

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.

Additional Info

  • tutorial_title:
Last modified on Wednesday, 12 June 2024 01:27