How to use Range.ExportAsFixedFormat in the xlwings API way

The ExportAsFixedFormat member of the Range object in Excel’s object model is a powerful method for converting a specified range of cells into a fixed-layout format, most commonly a PDF or XPS file. This functionality is essential for generating reports, invoices, or any document that requires a precise, non-editable, and easily shareable format from an Excel worksheet. In xlwings, this method is accessed through the api property, which provides direct access to the underlying Excel object model, allowing for precise control over the export process.

The syntax for calling ExportAsFixedFormat via xlwings is as follows:
range.api.ExportAsFixedFormat(Type, Filename, Quality, IncludeDocProperties, IgnorePrintAreas, From, To, OpenAfterPublish)
The parameters are:

  • Type: Required. Specifies the format type. Use 0 for PDF or 1 for XPS.
  • Filename: Optional. The full path and filename for the exported file. If omitted, Excel uses the workbook’s name and saves it in the current directory.
  • Quality: Optional. Can be 0 (Standard quality) or 1 (Minimum quality, smaller file size). Default is 0.
  • IncludeDocProperties: Optional. True to include document properties in the file, False otherwise. Default is False.
  • IgnorePrintAreas: Optional. True to ignore any print areas set in the worksheet and export the entire specified range, False to respect print areas. Default is False.
  • From and To: Optional. The starting and ending page numbers to export. If the Type is PDF, and From and To are omitted, the entire specified range is exported.
  • OpenAfterPublish: Optional. True to open the file after publishing, False otherwise. Default is False.

Here are practical xlwings code examples:

  1. Export a specific range to a PDF in the current directory:
import xlwings as xw
wb = xw.Book('report.xlsx')
sheet = wb.sheets['Data']
# Export range A1:D20
sheet.range('A1:D20').api.ExportAsFixedFormat(0, 'MyRangeReport.pdf')
  1. Export the entire used range of a sheet to a high-quality PDF with document properties, ignoring print areas, and open it after creation:
import xlwings as xw
wb = xw.Book('data.xlsx')
sheet = wb.sheets[0]
used_range = sheet.used_range
used_range.api.ExportAsFixedFormat(
Type=0,
Filename=r'C:\Reports\FullReport.pdf',
Quality=0,
IncludeDocProperties=True,
IgnorePrintAreas=True,
OpenAfterPublish=True
)
  1. Export a non-contiguous range (using Union) to a PDF:
import xlwings as xw
wb = xw.Book('analysis.xlsx')
sheet = wb.sheets['Summary']
range1 = sheet.range('A1:C10')
range2 = sheet.range('E1:G10')
# In xlwings, we typically export one rectangular range. To export multiple areas, you might need to create a temporary sheet or use the PrintArea property.
# A common workaround is to set a print area encompassing both ranges.
sheet.page_setup.print_area = 'A1:C10,E1:G10'
sheet.used_range.api.ExportAsFixedFormat(0, 'NonContiguous.pdf', IgnorePrintAreas=False)

October 28, 2026 (0)


Leave a Reply

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