Python: Insert or Remove Page Breaks in Excel

A page break is a markup that divides the content of a document or spreadsheet into multiple pages for printing or display. This feature can be used to adjust the page layout of a document to ensure that each page contains the appropriate information. By placing page breaks appropriately, you can also ensure that your document is presented in a better format and layout when printed. This article will explain how to insert or remove horizontal/vertical page breaks in Excel on the Python platform by using Spire.XLS for Python.

Install Spire.XLS for Python

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

pip install Spire.XLS

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

Insert Horizontal Page Breaks in Excel Using Python

Spire.XLS for Python supports inserting horizontal page breaks to specified cell ranges by calling Worksheet.HPageBreaks.Add(CellRange) method. The following are detailed steps.

  • Create a Workbook instance.
  • Load an Excel file from disk using Workbook.LoadFromFile() method.
  • Get a specified worksheet using Workbook.Worksheets[] property.
  • Insert horizontal page breaks to specified cell ranges using Worksheet.HPageBreaks.Add(CellRange) method.
  • Set view mode to Preview mode by Worksheet.ViewMode property.
  • Save the result file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

inputFile = "C:/Users/Administrator/Desktop/Sample.xlsx"
outputFile = "C:/Users/Administrator/Desktop/InsertHPageBreak.xlsx"

#Create a Workbook instance
workbook = Workbook()

#Load an Excel file from disk
workbook.LoadFromFile(inputFile)

#Get the first worksheet of this file
sheet = workbook.Worksheets[0]

#Insert horizontal page breaks to specified cell ranges
sheet.HPageBreaks.Add(sheet.Range["A12"])
sheet.HPageBreaks.Add(sheet.Range["A20"])

#Set view mode to Preview mode
sheet.ViewMode = ViewMode.Preview

#Save the result file
workbook.SaveToFile(outputFile, ExcelVersion.Version2013)
workbook.Dispose()

Python: Insert or Remove Page Breaks in Excel

Insert Vertical Page Breaks in Excel Using Python

Spire.XLS for Python also supports inserting vertical page breaks to specified cell ranges by calling Worksheet.VPageBreaks.Add(CellRange) method. The following are detailed steps.

  • Create a Workbook instance.
  • Load an Excel file from disk using Workbook.LoadFromFile() method.
  • Get a specified worksheet using Workbook.Worksheets[] property.
  • Insert vertical page breaks to specified cell ranges using Worksheet.VPageBreaks.Add(CellRange) method.
  • Set view mode to Preview mode using Worksheet.ViewMode property.
  • Save the result file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

inputFile = "C:/Users/Administrator/Desktop/Sample.xlsx"
outputFile = "C:/Users/Administrator/Desktop/InsertVPageBreak.xlsx"

#Create a Workbook instance
workbook = Workbook()

#Load an Excel file from disk
workbook.LoadFromFile(inputFile)

#Get the first worksheet of this file
sheet = workbook.Worksheets[0]

#Insert vertical page breaks to specified cell ranges
sheet.VPageBreaks.Add(sheet.Range["B1"])
sheet.VPageBreaks.Add(sheet.Range["D3"])

#Set view mode to Preview mode
sheet.ViewMode = ViewMode.Preview

#Save the result file
workbook.SaveToFile(outputFile, ExcelVersion.Version2013)
workbook.Dispose()

Python: Insert or Remove Page Breaks in Excel

Remove Horizontal Page Breaks from Excel Using Python

If you want to remove horizontal page breaks from Excel, call the Worksheet.HPageBreaks.RemoveAt() or Worksheet.HPageBreaks.Clear() methods. The following are detailed steps.

  • Create a Workbook instance.
  • Load an Excel file from disk using Workbook.LoadFromFile() method.
  • Get a specified worksheet using Workbook.Worksheets[] property.
  • Remove all the horizontal page breaks by calling Worksheet.HPageBreaks.Clear() method or remove a specific horizontal page break by calling Worksheet.HPageBreaks.RemoveAt() method.
  • Set view mode to Preview mode using Worksheet.ViewMode property.
  • Save the result file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

inputFile = "C:/Users/Administrator/Desktop/InsertHPageBreak.xlsx"
outputFile = "C:/Users/Administrator/Desktop/RemoveHPageBreak.xlsx"

#Create a Workbook instance
workbook = Workbook()

#Load an Excel file from disk
workbook.LoadFromFile(inputFile)

#Get the first worksheet from this file
sheet = workbook.Worksheets[0]

#Clear all the horizontal page breaks
#sheet.HPageBreaks.Clear()

#Remove the first horizontal page break
sheet.HPageBreaks.RemoveAt(0)

#Set view mode to Preview mode
sheet.ViewMode = ViewMode.Preview

#Save the result file
workbook.SaveToFile(outputFile, ExcelVersion.Version2013)
workbook.Dispose()

Python: Insert or Remove Page Breaks in Excel

Remove Vertical Page Breaks from Excel Using Python

If you want to remove vertical page breaks from Excel, call the Worksheet.VPageBreaks.RemoveAt() or Worksheet.VPageBreaks.Clear() methods. The following are detailed steps.

  • Create a Workbook instance.
  • Load an Excel file from disk using Workbook.LoadFromFile() method.
  • Get a specified worksheet using Workbook.Worksheets[] property.
  • Remove all the vertical page breaks by calling Worksheet.VPageBreaks.Clear() method or remove a specific vertical page break by calling Worksheet.VPageBreaks.RemoveAt() method.
  • Set view mode to Preview mode using Worksheet.ViewMode property.
  • Save the result file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

inputFile = "C:/Users/Administrator/Desktop/InsertVPageBreak.xlsx"
outputFile = "C:/Users/Administrator/Desktop/RemoveVPageBreak.xlsx"

#Create a Workbook instance
workbook = Workbook()

#Load an Excel file from disk
workbook.LoadFromFile(inputFile)

#Get the first worksheet from this file
sheet = workbook.Worksheets[0]

#Clear all the vertical page breaks
#sheet.VPageBreaks.Clear()

#Remove the first vertical page break
sheet.VPageBreaks.RemoveAt(0)

#Set view mode to Preview mode
sheet.ViewMode = ViewMode.Preview

#Save the result file
workbook.SaveToFile(outputFile, ExcelVersion.Version2013)
workbook.Dispose()

Python: Insert or Remove Page Breaks in Excel

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.