{"id":2316,"date":"2026-09-18T16:48:15","date_gmt":"2026-09-18T08:48:15","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2316"},"modified":"2026-03-28T13:00:21","modified_gmt":"2026-03-28T13:00:21","slug":"how-to-use-worksheetdisplaypagebreaks-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetdisplaypagebreaks-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.DisplayPageBreaks in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>DisplayPageBreaks<\/code> property of a <code>Worksheet<\/code> object in Excel is a Boolean value that controls whether page breaks are displayed on the worksheet. When set to <code>True<\/code>, Excel shows dotted lines indicating where pages will be divided when printed, based on current page setup settings like margins, orientation, and scaling. This is particularly useful for reviewing and adjusting print layouts before finalizing documents, ensuring content is properly distributed across pages. In xlwings, this property is accessible via the API, allowing automation of print preview adjustments directly from Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax in xlwings:<\/strong><br>The property can be accessed and modified using the <code>api<\/code> property of a worksheet object. The general syntax is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>worksheet.api.DisplayPageBreaks<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is a read\/write property, meaning you can both retrieve its current value and set it to a new Boolean value (<code>True<\/code> or <code>False<\/code>). No parameters are required for this property, as it simply toggles the display state. Note that <code>worksheet<\/code> refers to an xlwings <code>Sheet<\/code> object, typically obtained through <code>book.sheets['SheetName']<\/code> or <code>book.sheets[0]<\/code>.<\/p>\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>Check the current display status of page breaks:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n# Connect to an existing workbook or create a new one\nwb = xw.Book('example.xlsx')\nws = wb.sheets&#91;'Sheet1']\n# Get the current DisplayPageBreaks setting\ncurrent_setting = ws.api.DisplayPageBreaks\nprint(f\"Page breaks are displayed: {current_setting}\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This code snippet opens an Excel file, accesses a specific worksheet, and prints whether page breaks are currently visible.<\/p>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Enable the display of page breaks:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('example.xlsx')\nws = wb.sheets&#91;'Sheet1']\n# Set DisplayPageBreaks to True to show page breaks\nws.api.DisplayPageBreaks = True\nprint(\"Page breaks are now visible on the worksheet.\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After running this, the worksheet will immediately show dotted page break lines in the Excel interface, assuming page breaks are defined (e.g., via manual insertion or automatic calculation based on print area).<\/p>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Disable the display of page breaks:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('example.xlsx')\nws = wb.sheets&#91;'Sheet1']\n# Set DisplayPageBreaks to False to hide page breaks\nws.api.DisplayPageBreaks = False\nprint(\"Page breaks are now hidden on the worksheet.\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This hides the page break lines, which can clean up the view when focusing on data entry or analysis rather than printing layout.<\/p>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Toggle the display based on a condition:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('example.xlsx')\nws = wb.sheets&#91;'Sheet1']\n# Toggle the current state\nws.api.DisplayPageBreaks = not ws.api.DisplayPageBreaks\nprint(f\"Toggled display. Now set to: {ws.api.DisplayPageBreaks}\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `DisplayPageBreaks` property of a `Worksheet` object in Excel is a Boolean value that controls w&#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-2316","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2316","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=2316"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2316\/revisions"}],"predecessor-version":[{"id":3531,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2316\/revisions\/3531"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}