PageBreak — Aspose.Cells FOSS for Python API Reference
aspose-cells-foss allows inserting manual page breaks to control how a worksheet is divided across printed pages. Horizontal page breaks split the sheet between rows; vertical page breaks split it between columns. Both collections use 0-based indices.
Package: aspose.cells_foss
from aspose.cells_foss import Workbook
wb = Workbook("report.xlsx")
ws = wb.worksheets[0]
hpb = ws.horizontal_page_breaks
vpb = ws.vertical_page_breaksHorizontalPageBreakCollection
Accessed as ws.horizontal_page_breaks. A horizontal page break inserted before row n causes the sheet to start a new page at that row when printed.
All row references are 0-based (row 0 = row 1 in Excel).
Methods
add
hpb.add(row_or_cell: int | str) -> intAdds a page break before the specified row. Returns the zero-based index of the new break in the collection.
| Parameter | Type | Description |
|---|---|---|
row_or_cell | int | 0-based row index at which to insert the break. |
row_or_cell | str | A1-style cell reference; the row of this cell is used (e.g., "A11" → break before row 10 in 0-based terms). |
remove
hpb.remove(row_or_cell: int | str)Removes the page break associated with the given row index or cell reference.
remove_at
hpb.remove_at(index: int)Removes the page break at the given zero-based position in the collection.
clear
hpb.clear()Removes all horizontal page breaks from the worksheet.
to_list
hpb.to_list() -> listReturns a plain Python list of the current break row indices (0-based integers), sorted in ascending order.
Properties and Iteration
| Member | Description |
|---|---|
count | Number of horizontal page breaks (read-only). |
__len__() | Returns count. |
__iter__() | Iterates over break row indices. |
__getitem__(index) | Returns the break row index at position index in the collection. |
VerticalPageBreakCollection
Accessed as ws.vertical_page_breaks. A vertical page break inserted before column n causes the sheet to start a new page at that column when printed.
All column references are 0-based (column 0 = column A).
Methods
add
vpb.add(column_or_cell: int | str) -> intAdds a page break before the specified column. Returns the zero-based index of the new break in the collection.
| Parameter | Type | Description |
|---|---|---|
column_or_cell | int | 0-based column index at which to insert the break. |
column_or_cell | str | A1-style cell reference; the column of this cell is used (e.g., "E1" → break before column 4 in 0-based terms). |
remove
vpb.remove(column_or_cell: int | str)Removes the page break associated with the given column index or cell reference.
clear
vpb.clear()Removes all vertical page breaks from the worksheet.
to_list
vpb.to_list() -> listReturns a plain Python list of the current break column indices (0-based integers), sorted in ascending order.
Properties and Iteration
| Member | Description |
|---|---|
count | Number of vertical page breaks (read-only). |
__len__() | Returns count. |
__iter__() | Iterates over break column indices. |
__getitem__(index) | Returns the break column index at position index in the collection. |
Example
The following example inserts horizontal page breaks every 30 rows to produce evenly divided print pages on a large report.
from aspose.cells_foss import Workbook
wb = Workbook()
ws = wb.worksheets[0]
ws.name = "AnnualReport"
# Write 120 rows of placeholder data
for row in range(120):
ws.cells[row][0].put_value(f"Row {row + 1}")
ws.cells[row][1].put_value(row * 100)
# Insert a page break before rows 30, 60, and 90 (0-based)
ws.horizontal_page_breaks.add(30)
ws.horizontal_page_breaks.add(60)
ws.horizontal_page_breaks.add(90)
print(f"Page breaks at rows: {ws.horizontal_page_breaks.to_list()}")
# Output: Page breaks at rows: [30, 60, 90]
# Add a vertical break to split wide reports after column D (index 4)
ws.vertical_page_breaks.add(4)
wb.save("annual_report_paged.xlsx")Removing a Specific Break
from aspose.cells_foss import Workbook
wb = Workbook("annual_report_paged.xlsx")
ws = wb.worksheets[0]
# Remove the break at row 60 (0-based)
ws.horizontal_page_breaks.remove(60)
print(f"Remaining breaks: {ws.horizontal_page_breaks.to_list()}")
# Output: Remaining breaks: [30, 90]
wb.save("annual_report_paged.xlsx")