The HPageBreaks member of the Worksheet object in the Excel object model provides access to the collection of horizontal page breaks within a worksheet. In xlwings, this collection is accessible via the api property, which exposes the underlying Excel VBA object model. This allows for programmatic control over where pages break when the worksheet is printed, enabling precise formatting for reports and documents. The primary use is to add, delete, or query horizontal page breaks, which are essential for managing print layout in multi-page data sets.
Syntax and Parameters
In xlwings, you access the HPageBreaks collection through a worksheet object’s api property:
hpagebreaks = ws.api.HPageBreaks
The collection is 1-indexed, similar to Excel VBA. Key methods and properties include:
Add(Before): Adds a new horizontal page break.Before: A required parameter of typeObject. It specifies the range above which the page break will be inserted. You typically pass an xlwingsRangeobject’s.apiproperty (e.g.,ws.range("A10").api). The break is inserted above the top edge of this range.Count(Property): Returns aLongrepresenting the number of horizontal page breaks in the collection.Item(Index): Returns a singleHPageBreakobject from the collection.Index: The index number of the page break (1-indexed).Location(Property of anHPageBreakobject): Returns aRangeobject representing the cell where the page break is set (the cell immediately below the break line). This is read/write, allowing you to move an existing break.
Code Examples
- Adding a Horizontal Page Break:
Inserts a horizontal page break above row 15.
import xlwings as xw
wb = xw.Book("report.xlsx")
ws = wb.sheets["Sheet1"]
# Add a page break above cell A15
ws.api.HPageBreaks.Add(Before=ws.range("A15").api)
- Counting and Listing Page Breaks:
Prints the count and location of each horizontal page break.
import xlwings as xw
wb = xw.Book.active
ws = wb.sheets[0]
hbreaks = ws.api.HPageBreaks
print(f"Number of horizontal page breaks: {hbreaks.Count}")
for i in range(1, hbreaks.Count + 1):
break_obj = hbreaks.Item(i)
# The Location property returns the cell below the break
location_cell = break_obj.Location.Address
print(f" Break {i}: Above row {break_obj.Location.Row} (at {location_cell})")
- Deleting All Horizontal Page Breaks:
Clears all manually set horizontal page breaks from the sheet. Note: This does not remove automatic breaks inserted by Excel based on page margins and size.
import xlwings as xw
wb = xw.Book.active
ws = wb.sheets[0]
hbreaks = ws.api.HPageBreaks
# Loop backwards to avoid index shifting when deleting
for i in range(hbreaks.Count, 0, -1):
# The HPageBreak object itself doesn't have a .Delete() method.
# You delete it by clearing the break from its location.
hbreaks.Item(i).Location.PageBreak = -4142 # xlPageBreakNone
# Alternatively, reset all page breaks on the sheet:
# ws.api.ResetAllPageBreaks()
- Moving an Existing Page Break:
Changes the position of the first horizontal page break to be above row 25.
import xlwings as xw
wb = xw.Book.active
ws = wb.sheets[0]
hbreaks = ws.api.HPageBreaks
if hbreaks.Count >= 1:
first_break = hbreaks.Item(1)
first_break.Location = ws.range("A25").api
Leave a Reply