🔥 How to Convert Word (Doc/ Docx) to PDF in Python?
Published: 2023-12-07 07:30:10

Converting Word to PDF is a common task in our daily life as it it brings us several benefits such as:👇
**Wide compatibility:** PDF supports a wide range of operating systems, devices and software applications. By converting Word documents to PDF, you can ensure consistent viewing and accessing of files across different platforms without any formatting or compatibility issues.

**Reduce file size:** PDF files tend to be smaller than Word documents. Converting Word to PDF makes files easier to share via email or upload to a website.

**Easy to print:** PDF is designed for high quality printing. Converting a Word document to PDF ensures that the layout, fonts, and images of the document will be consistent when printed, no matter what printer or settings are used.

✨ In this article, we will explore how to programmatically **convert Word (Doc/ Docx) to PDF in Python** using third-party Python library ([Spire.Doc for Python](https://www.e-iceblue.com/Introduce/doc-for-python.html)). The following is the pip command to install it:
```
pip install Spire.Doc
```

**<u>Let's start!</u>**

### 🚀Convert Word to PDF with Python
```python
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()
```

In the ablove code, we just load a .doc or .docx file and then it can be converted to a PDF file through the **Document.SaveToFile()** method.
The result file:

![WordtoPDF](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5vp7atabbsoh73sj9ak.png)

---

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.

```python
# 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.
```python
# Create a ToPdfParameterList object
parameter = ToPdfParameterList()
# Embed fonts in PDF
parameter.IsEmbeddedAllFonts = True
```

⚡ In addition, **the bookmarks of the original Word document can also be preserved** when converting, or we can **create new bookmarks based on the headings**.
```python
# Create a ToPdfParameterList object
parames = ToPdfParameterList()
# Create bookmarks from Word headings
parames.CreateWordBookmarksUsingHeadings = True
# Create bookmarks in PDF from existing bookmarks in Word
parames.CreateWordBookmarks = True
# Save the document to PDF
document.SaveToFile("output/ToPdfWithBookmarks.pdf", FileFormat.PDF)
document.Close()
```

_Refer: [https://www.e-iceblue.com/Introduce/doc-for-python.html](https://www.e-iceblue.com/Introduce/doc-for-python.html)_

#### Also Learn:
> [Convert Word to Images in Python ](https://www.e-iceblue.com/Tutorials/Python/Spire.Doc-for-Python/Program-Guide/Conversion/Python-Convert-Word-to-Images.html)
> [Convert Word to HTML in Python ](https://www.e-iceblue.com/Tutorials/Python/Spire.Doc-for-Python/Program-Guide/Conversion/Python-Convert-Word-to-HTML.html)
> [Convert HTML to Word in Python ](https://www.e-iceblue.com/Tutorials/Python/Spire.Doc-for-Python/Program-Guide/Conversion/Python-Convert-HTML-to-Word.html)
> [Create, Read, or Update a Word Document in Python ](https://www.e-iceblue.com/Tutorials/Python/Spire.Doc-for-Python/Program-Guide/Document-Operation/Python-Create-Read-or-Update-a-Word-Document.html)