{"id":2289,"date":"2026-09-05T07:02:05","date_gmt":"2026-09-04T23:02:05","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2289"},"modified":"2026-03-28T12:32:27","modified_gmt":"2026-03-28T12:32:27","slug":"how-to-use-worksheetprintout-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetprintout-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.PrintOut in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>PrintOut<\/code> method of the <code>Worksheet<\/code> object in the Excel object model is a powerful feature for programmatically printing worksheets. In xlwings, this functionality is exposed through the <code>api<\/code> property, which provides direct access to the underlying Excel VBA object model. This allows for precise control over printing parameters, enabling automation of printing tasks directly from Python scripts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality<\/strong><br>The <code>PrintOut<\/code> method prints the specified worksheet. It offers a range of optional parameters to control aspects such as the number of copies, print preview, printer selection, print range, and output to a file. This is essential for automating report generation, batch printing, or creating printed outputs from data processed in Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax<\/strong><br>In xlwings, the method is accessed via the worksheet&#8217;s <code>api<\/code> object. The full syntax, with parameters mapped from the VBA method, is as follows:<br><code>worksheet.api.PrintOut(From, To, Copies, Preview, ActivePrinter, PrintToFile, Collate, PrToFileName, IgnorePrintAreas)<\/code><br>Where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>From<\/code>: (Optional) The page number from which to start printing. If omitted, printing starts from the beginning.<\/li>\n\n\n\n<li><code>To<\/code>: (Optional) The page number on which to stop printing. If omitted, printing goes to the end.<\/li>\n\n\n\n<li><code>Copies<\/code>: (Optional) The number of copies to print. If omitted, one copy is printed.<\/li>\n\n\n\n<li><code>Preview<\/code>: (Optional) <code>True<\/code> to have Excel invoke print preview before printing. <code>False<\/code> (default) to print immediately.<\/li>\n\n\n\n<li><code>ActivePrinter<\/code>: (Optional) Sets the name of the active printer.<\/li>\n\n\n\n<li><code>PrintToFile<\/code>: (Optional) <code>True<\/code> to print to a file. If <code>PrToFileName<\/code> is not specified, Excel prompts the user for the output filename.<\/li>\n\n\n\n<li><code>Collate<\/code>: (Optional) <code>True<\/code> (default) to collate multiple copies.<\/li>\n\n\n\n<li><code>PrToFileName<\/code>: (Optional) If <code>PrintToFile<\/code> is set to <code>True<\/code>, this specifies the path and filename of the output file (e.g., <code>\"C:\\Output.pdf\"<\/code>).<\/li>\n\n\n\n<li><code>IgnorePrintAreas<\/code>: (Optional) <code>True<\/code> to ignore any print areas set in the worksheet and print the entire sheet.<\/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>Basic Print:<\/strong> Print the active worksheet immediately to the default printer.<\/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;\"Data\"]\nws.api.PrintOut()<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Print with Preview and Copies:<\/strong> Print two collated copies of the worksheet after showing the print preview.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>ws.api.PrintOut(Copies=2, Preview=True, Collate=True)<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Print Specific Pages:<\/strong> Print only pages 2 through 4 of the worksheet.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>ws.api.PrintOut(From=2, To=4)<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Print to PDF File:<\/strong> Print the entire worksheet to a PDF file, ignoring any set print area.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>output_path = r\"C:\\Reports\\output.pdf\"\nws.api.PrintOut(PrintToFile=True, PrToFileName=output_path, IgnorePrintAreas=True)<\/code><\/pre>\n\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li><strong>Using Named Arguments for Clarity:<\/strong> Explicitly naming arguments is recommended for readability.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>ws.api.PrintOut(From=1, To=1, Copies=1, Preview=False, Collate=True, IgnorePrintAreas=False)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `PrintOut` method of the `Worksheet` object in the Excel object model is a powerful feature for &#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-2289","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2289","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=2289"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2289\/revisions"}],"predecessor-version":[{"id":3492,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2289\/revisions\/3492"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}