{"id":2325,"date":"2026-09-23T07:44:28","date_gmt":"2026-09-22T23:44:28","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2325"},"modified":"2026-03-28T13:16:48","modified_gmt":"2026-03-28T13:16:48","slug":"how-to-use-worksheethpagebreaks-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheethpagebreaks-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.HPageBreaks in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <strong>HPageBreaks<\/strong> member of the <strong>Worksheet<\/strong> 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 <code>api<\/code> 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax and Parameters<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In xlwings, you access the <code>HPageBreaks<\/code> collection through a worksheet object&#8217;s <code>api<\/code> property:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hpagebreaks = ws.api.HPageBreaks<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The collection is 1-indexed, similar to Excel VBA. Key methods and properties include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>Add(Before)<\/code><\/strong>: Adds a new horizontal page break.<\/li>\n\n\n\n<li><code>Before<\/code>: A required parameter of type <code>Object<\/code>. It specifies the range above which the page break will be inserted. You typically pass an xlwings <code>Range<\/code> object&#8217;s <code>.api<\/code> property (e.g., <code>ws.range(\"A10\").api<\/code>). The break is inserted above the top edge of this range.<\/li>\n\n\n\n<li><strong><code>Count<\/code><\/strong> (Property): Returns a <code>Long<\/code> representing the number of horizontal page breaks in the collection.<\/li>\n\n\n\n<li><strong><code>Item(Index)<\/code><\/strong>: Returns a single <code>HPageBreak<\/code> object from the collection.<\/li>\n\n\n\n<li><code>Index<\/code>: The index number of the page break (1-indexed).<\/li>\n\n\n\n<li><strong><code>Location<\/code><\/strong> (Property of an <code>HPageBreak<\/code> object): Returns a <code>Range<\/code> object 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.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Adding a Horizontal Page Break:<\/strong><br>Inserts a horizontal page break above row 15.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book(\"report.xlsx\")\nws = wb.sheets&#91;\"Sheet1\"]\n# Add a page break above cell A15\nws.api.HPageBreaks.Add(Before=ws.range(\"A15\").api)<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Counting and Listing Page Breaks:<\/strong><br>Prints the count and location of each horizontal page break.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book.active\nws = wb.sheets&#91;0]\nhbreaks = ws.api.HPageBreaks\nprint(f\"Number of horizontal page breaks: {hbreaks.Count}\")\nfor i in range(1, hbreaks.Count + 1):\n    break_obj = hbreaks.Item(i)\n    # The Location property returns the cell below the break\n    location_cell = break_obj.Location.Address\n    print(f\" Break {i}: Above row {break_obj.Location.Row} (at {location_cell})\")<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Deleting All Horizontal Page Breaks:<\/strong><br>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.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book.active\nws = wb.sheets&#91;0]\nhbreaks = ws.api.HPageBreaks\n# Loop backwards to avoid index shifting when deleting\nfor i in range(hbreaks.Count, 0, -1):\n    # The HPageBreak object itself doesn't have a .Delete() method.\n    # You delete it by clearing the break from its location.\n    hbreaks.Item(i).Location.PageBreak = -4142 # xlPageBreakNone\n    # Alternatively, reset all page breaks on the sheet:\n    # ws.api.ResetAllPageBreaks()<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Moving an Existing Page Break:<\/strong><br>Changes the position of the first horizontal page break to be above row 25.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book.active\nws = wb.sheets&#91;0]\nhbreaks = ws.api.HPageBreaks\nif hbreaks.Count >= 1:\n    first_break = hbreaks.Item(1)\n    first_break.Location = ws.range(\"A25\").api<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The **HPageBreaks** member of the **Worksheet** object in the Excel object model provides access to &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-2325","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/comments?post=2325"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2325\/revisions"}],"predecessor-version":[{"id":3557,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2325\/revisions\/3557"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}