{"id":2416,"date":"2026-11-07T15:06:30","date_gmt":"2026-11-07T07:06:30","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2416"},"modified":"2026-03-29T10:33:20","modified_gmt":"2026-03-29T10:33:20","slug":"how-to-use-rangeprintout-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-rangeprintout-in-the-xlwings-api-way\/","title":{"rendered":"How to use Range.PrintOut in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>PrintOut<\/code> member of the <code>Range<\/code> object in Excel&#8217;s object model is a method used to print a specified range of cells. In xlwings, this functionality is accessible through the <code>api<\/code> property, which provides direct access to the underlying Excel object model. This method is particularly useful for automating the printing of specific data sections, reports, or charts directly from Python scripts, enabling seamless integration of Excel printing tasks into automated workflows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality<\/strong><br>The <code>PrintOut<\/code> method prints the specified range. It allows control over various print settings such as the number of copies, whether to collate, and the printer to use. This can help in generating hard copies of data without manual intervention, ideal for batch processing or scheduled reporting.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax<\/strong><br>In xlwings, the syntax for calling <code>PrintOut<\/code> is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range_object.api.PrintOut(From, To, Copies, Preview, ActivePrinter, PrintToFile, Collate, PrToFileName)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>range_object<\/code> is an xlwings Range object (e.g., <code>sheet.range('A1:D10')<\/code>). The parameters are optional and map directly to Excel&#8217;s <code>PrintOut<\/code> method arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>From<\/code>: The starting page number to print. If omitted, printing starts from the beginning.<\/li>\n\n\n\n<li><code>To<\/code>: The ending page number to print. If omitted, printing goes to the end.<\/li>\n\n\n\n<li><code>Copies<\/code>: The number of copies to print. Default is 1.<\/li>\n\n\n\n<li><code>Preview<\/code>: If <code>True<\/code>, Excel will show the print preview instead of printing immediately. Default is <code>False<\/code>.<\/li>\n\n\n\n<li><code>ActivePrinter<\/code>: A string specifying the printer name to use. If omitted, the default printer is used.<\/li>\n\n\n\n<li><code>PrintToFile<\/code>: If <code>True<\/code>, the output is sent to a file. Default is <code>False<\/code>.<\/li>\n\n\n\n<li><code>Collate<\/code>: If <code>True<\/code>, copies are collated when printing multiple copies. Default is <code>True<\/code>.<\/li>\n\n\n\n<li><code>PrToFileName<\/code>: The file name if <code>PrintToFile<\/code> is set to <code>True<\/code>. If omitted, Excel prompts for a file name.<\/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 printing of a range:<\/strong><br>This example prints the range A1 to D10 on the active sheet with default settings.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n# Connect to the active workbook and sheet\nwb = xw.books.active\nsheet = wb.sheets.active\n# Define the range and print\nprint_range = sheet.range('A1:D10')\nprint_range.api.PrintOut()<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Printing multiple copies with collation:<\/strong><br>Here, two collated copies of the range B2:F20 are printed.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.books.active\nsheet = wb.sheets.active\nprint_range = sheet.range('B2:F20')\nprint_range.api.PrintOut(Copies=2, Collate=True)<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Printing to a file:<\/strong><br>This sends the output of the range A1:Z100 to a PDF file named &#8220;report.pdf&#8221; without showing preview.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.books.active\nsheet = wb.sheets.active\nprint_range = sheet.range('A1:Z100')\nprint_range.api.PrintOut(PrintToFile=True, PrToFileName=\"report.pdf\", Preview=False)<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Using print preview:<\/strong><br>To preview the print output for the range C5:G15 before printing.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.books.active\nsheet = wb.sheets.active\nprint_range = sheet.range('C5:G15')\nprint_range.api.PrintOut(Preview=True)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `PrintOut` member of the `Range` object in Excel&apos;s object model is a method used to print a spec&#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-2416","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2416","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=2416"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2416\/revisions"}],"predecessor-version":[{"id":3672,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2416\/revisions\/3672"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}