How to Convert Word to PDF Using Python

JonathonJonathon
2 min read

Converting Word documents to PDF format is a common practice as it allows us to maintain the integrity of the document when sharing. In addition, converting Word to PDF ensures cross-platform compatibility and provides other advantages such as security and permissions control. In this article, we will explore how to convert Word (Doc/ Docx) to PDF in Python using a Python Word library.

Install the Python Library

The solution uses the Spire.Doc for Python library. It supports a rich set of Word document processing features, and we can install it easily in your VS Code using the following pip command.

pip install Spire.Doc

A step-by-step installation tutorial can be found at: https://www.e-iceblue.com/Tutorials/Python/Spire.Doc-for-Python/Getting-Started/How-to-Install-Spire.Doc-for-Python-in-VS-Code.html

Python - Convert Word to PDF

To accomplish this task, we just need to load a .doc or .docx document and then convert it to PDF through the Document.SaveToFile(string fileName, FileFormat.PDF) method.

Python code:

from spire.doc import *
from spire.doc.common import *

# Create word document
document = Document()

# Load a doc or docx file
document.LoadFromFile("sample.docx")

#Save the document to PDF
document.SaveToFile("ToPDF.pdf", FileFormat.PDF)
document.Close()

The Python Word API also offers the ToPdfParameterList class that allows us to convert Word to PDF with additional conversion settings. We can encrypt the generated PDF file using the below code.

# Set open password and permission password for the generated PDF
openPsd = "abcde"
permissionPsd = "12345"
parameter.PdfSecurity.Encrypt(openPsd, permissionPsd, PdfPermissionsFlags.Default, PdfEncryptionKeySize.Key128Bit)

# Save the Word document to PDF
document.SaveToFile("ToPdfWithPassword.pdf", parameter)

Embed fonts in the generated PDF to ensure consistent document appearance.

# Create a ToPdfParameterList object
parameter = ToPdfParameterList()

# Embed fonts in PDF
parameter.IsEmbeddedAllFonts = True
0
Subscribe to my newsletter

Read articles from Jonathon directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jonathon
Jonathon