Convert Word (.docx) to PDF in Python: A Step Guide – Python Tutorial

By | February 10, 2022

The simplest way to convert a .docx file to pdf is to use python docx2pdf library. In this tutorial, we will use an example to show you how to do.

Install docx2pdf

We can use pip command to install, here is an example:

pip install docx2pdf

pip install docx2pdf

Convert .docx to pdf in python

Then, we can .docx to pdf using docx2pdf.convert() function. This function is defined as:

def convert(input_path, output_path=None, keep_active=False):

Here input_path is the path of .docx file.

output_path is the path of pdf file.

Here is an example to show you how to use this function.

import docx2pdf 

word_file = r'src.docx'
pdf_file = 'study.pdf'
with open(pdf_file, "wb") as f:
    pass
docx2pdf.convert(word_file, pdf_file)

In this example, we should create an empty pdf file, then we can convert a word file to it.

convert .docx file to pdf in python

Leave a Reply