How to Export Documents to PDF in Python

How to Export Documents to PDF in Python

Aspose.Words FOSS for Python exports loaded Word documents to PDF through a built-in rendering pipeline. The library reads DOCX, DOC, RTF, TXT, and Markdown as input formats; PDF is an output-only format.


Basic PDF Export

PDF is an export-only format. Save the document to PDF using Document.save():

import aspose.words_foss as aw

doc = aw.Document("report.docx")
doc.save("report.pdf")

The output format is determined by the file extension. The PDF writer preserves text formatting, tables, images, and page layout from the source document.


Using PdfSaveOptions

For finer control over PDF output, pass a PdfSaveOptions instance:

import aspose.words_foss as aw
from aspose.words_foss.saving import PdfSaveOptions

doc = aw.Document("input.docx")
opts = PdfSaveOptions()
doc.save("output.pdf", opts)

Batch Conversion

Convert multiple files in a loop:

import aspose.words_foss as aw
from pathlib import Path

for src in Path("docs/").glob("*.docx"):
    doc = aw.Document(str(src))
    doc.save(str(src.with_suffix(".pdf")))
    print(f"Converted {src.name}")

See Also