{"id":2334,"date":"2026-09-27T16:00:01","date_gmt":"2026-09-27T08:00:01","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2334"},"modified":"2026-03-28T13:16:33","modified_gmt":"2026-03-28T13:16:33","slug":"how-to-use-worksheetpagesetup-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetpagesetup-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.PageSetup in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The PageSetup member of the Worksheet object in xlwings provides comprehensive control over printing settings, allowing users to configure page layout, margins, orientation, scaling, headers, footers, and other print-related properties programmatically. This is essential for generating professional reports and ensuring printed documents match specific formatting requirements. Through xlwings, you can access these settings using the <code>api<\/code> property to interact with the underlying Excel object model, enabling automation of print preparation tasks directly from Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality:<\/strong><br>The PageSetup object is used to manage all aspects of page setup for printing a worksheet. Key functionalities include setting page orientation (portrait or landscape), adjusting margins (top, bottom, left, right, header, footer), defining paper size, scaling printouts (by percentage or to fit a certain number of pages), and configuring headers and footers with text, page numbers, dates, or other elements. It also allows control over print titles, gridlines, and comments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:<\/strong><br>In xlwings, you access the PageSetup member via the worksheet object&#8217;s <code>api<\/code> property. The general syntax is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>worksheet.api.PageSetup.PropertyName\nworksheet.api.PageSetup.MethodName(Arguments)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Where <code>worksheet<\/code> is an xlwings Worksheet object. Properties can be get or set, while methods perform actions. Common properties and methods include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Properties:<\/strong> <code>Orientation<\/code>, <code>Zoom<\/code>, <code>FitToPagesTall<\/code>, <code>FitToPagesWide<\/code>, <code>TopMargin<\/code>, <code>BottomMargin<\/code>, <code>LeftMargin<\/code>, <code>RightMargin<\/code>, <code>HeaderMargin<\/code>, <code>FooterMargin<\/code>, <code>PaperSize<\/code>, <code>PrintTitleRows<\/code>, <code>PrintTitleColumns<\/code>, <code>PrintGridlines<\/code>, <code>PrintHeadings<\/code>, <code>LeftHeader<\/code>, <code>CenterHeader<\/code>, <code>RightHeader<\/code>, <code>LeftFooter<\/code>, <code>CenterFooter<\/code>, <code>RightFooter<\/code>.<\/li>\n\n\n\n<li><strong>Methods:<\/strong> <code>PrintPreview()<\/code>, <code>PrintOut()<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Parameters for methods like <code>PrintOut()<\/code> typically include: <code>From<\/code>, <code>To<\/code>, <code>Copies<\/code>, <code>Preview<\/code>, <code>ActivePrinter<\/code>, <code>PrintToFile<\/code>, <code>Collate<\/code>, <code>PrToFileName<\/code>. These can be specified as keyword arguments. Property values are often integers or strings; for example, <code>Orientation<\/code> can be set to <code>1<\/code> for portrait or <code>2<\/code> for landscape, and margins are in points.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Examples:<\/strong><br>Here are xlwings API code instances demonstrating the use of PageSetup:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Setting Page Orientation and Scaling:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('example.xlsx')\nsheet = wb.sheets&#91;'Sheet1']\n\n# Set landscape orientation\nsheet.api.PageSetup.Orientation = 2 # 2 for landscape, 1 for portrait\n\n# Set zoom to 80%\nsheet.api.PageSetup.Zoom = 80\n\n# Scale to fit to 1 page wide and 1 page tall\nsheet.api.PageSetup.FitToPagesTall = 1\nsheet.api.PageSetup.FitToPagesWide = 1<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Adjusting Margins:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Set margins in points (1 point = 1\/72 inch)\nsheet.api.PageSetup.TopMargin = 50\nsheet.api.PageSetup.BottomMargin = 50\nsheet.api.PageSetup.LeftMargin = 70\nsheet.api.PageSetup.RightMargin = 70\nsheet.api.PageSetup.HeaderMargin = 30\nsheet.api.PageSetup.FooterMargin = 30<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Configuring Headers and Footers:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Set header and footer text\nsheet.api.PageSetup.LeftHeader = \"&amp;LReport Date: &amp;D\"\nsheet.api.PageSetup.CenterHeader = \"&amp;CSales Data\"\nsheet.api.PageSetup.RightHeader = \"&amp;RPage &amp;P of &amp;N\"\nsheet.api.PageSetup.LeftFooter = \"&amp;LConfidential\"\nsheet.api.PageSetup.CenterFooter = \"&amp;C&amp;F\"\nsheet.api.PageSetup.RightFooter = \"&amp;RPrinted at &amp;T\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>&amp;L<\/code>, <code>&amp;C<\/code>, <code>&amp;R<\/code> align left, center, right; <code>&amp;D<\/code> is current date, <code>&amp;T<\/code> is current time, <code>&amp;P<\/code> is page number, <code>&amp;N<\/code> is total pages, <code>&amp;F<\/code> is file name.<\/p>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Setting Print Titles and Gridlines:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Set rows 1:1 as print titles\nsheet.api.PageSetup.PrintTitleRows = \"$1:$1\"\n\n# Print gridlines\nsheet.api.PageSetup.PrintGridlines = True\n\n# Print row and column headings\nsheet.api.PageSetup.PrintHeadings = True<\/code><\/pre>\n\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li><strong>Printing the Worksheet:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Preview print\nsheet.api.PageSetup.PrintPreview()\n\n# Print directly (optional parameters)\nsheet.api.PageSetup.PrintOut(From=1, To=1, Copies=2, Preview=False, Collate=True)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The PageSetup member of the Worksheet object in xlwings provides comprehensive control over printing&#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-2334","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2334","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=2334"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2334\/revisions"}],"predecessor-version":[{"id":3556,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2334\/revisions\/3556"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}