How to use Worksheets.PrintOut in the xlwings API way

The PrintOut method in the Worksheets collection, accessible via the xlwings API, provides a powerful way to print worksheets directly from Python. This functionality is essential for automating report generation and document distribution workflows, allowing you to control various aspects of the printing process without manual intervention in Excel.

Functionality
The PrintOut method sends one or more worksheets to a printer. You can specify a range of pages, the number of copies, whether to print to a file, and other standard print settings. It is particularly useful for batch printing multiple sheets or generating physical/digital copies of reports as part of an automated script.

Syntax in xlwings
The method is called on a Sheet object (representing a single worksheet) or can be applied to multiple sheets via the sheets collection. The xlwings syntax mirrors the VBA object model closely.

sheet.api.PrintOut(From, To, Copies, Preview, ActivePrinter, PrintToFile, Collate, PrToFileName, IgnorePrintAreas)

Parameters and Their Meanings

ParameterData TypeDescriptionHow to Access/Example Value
FromIntegerThe page number at which to start printing. If omitted, printing starts from the beginning.Set to a number, e.g., 1.
ToIntegerThe page number at which to stop printing. If omitted, printing goes to the last page.Set to a number, e.g., 3.
CopiesIntegerThe number of copies to print. Default is 1.Set to a number, e.g., 2.
PreviewBooleanTrue to have Excel invoke print preview before printing. False (default) to print immediately.Use True or False.
ActivePrinterStringSets the name of the active printer.Provide a printer name string. Often left as None to use default.
PrintToFileBooleanTrue to print to a file. If True, you must specify PrToFileName.Use True or False.
CollateBooleanTrue (default) to collate multiple copies.Use True or False.
PrToFileNameStringIf PrintToFile is True, this is the name of the file to print to.Provide a full file path string.
IgnorePrintAreasBooleanTrue to ignore any print areas set in the worksheet and print the entire sheet.Use True or False.

Most parameters are optional. In xlwings, you pass them as keyword arguments, and you can omit any you don’t need, using Excel’s defaults.

Code Examples

  1. Basic Print: Print the active sheet immediately to the default printer.
import xlwings as xw
wb = xw.Book('report.xlsx')
wb.sheets['Sheet1'].api.PrintOut()
  1. Print Multiple Copies with Collation: Print two collated copies of a specific page range from “DataSheet”.
sheet = wb.sheets['DataSheet']
sheet.api.PrintOut(From=1, To=1, Copies=2, Collate=True)
  1. Print to a PDF File: Print the entire “Summary” worksheet to a PDF file, ignoring any set print area.
output_path = r'C:\Reports\summary.pdf'
wb.sheets['Summary'].api.PrintOut(PrintToFile=True, PrToFileName=output_path, IgnorePrintAreas=True)
  1. Print Preview: Open the print preview for the first three sheets without actually printing.
for sheet in wb.sheets[:3]: # Loop through first three sheets
sheet.api.PrintOut(Preview=True, Copies=1)

August 22, 2026 (0)


Leave a Reply

Your email address will not be published. Required fields are marked *