How to convert docx to pdf in Python

In this example we will see how to convert docx to pdf in Python programming language. Your word document may contain images, paragraphs, headings, text, table, title etc. This program will put them into a pdf file. Note that this program will convert only word document of docx type.

This is a very simple program with just one line of code will do your job. I am using here docx2pdf module to convert word document into pdf file.

Prerequisites

Python 3.8.0, docx2pdf

You can install docx2pdf module using the command pip install docx2pdf. Make sure you run this command in Administrator mode.

Convert docx to pdf

Now we will create Python script to convert docx file to pdf file. You can give name of the file anything you want with the following code.

The convert function takes two parameters – source file location and output file location.

In the below source, the input file is in the current directory where my Python script is kept.

from docx2pdf import convert

convert("FitnessBuddy.docx", "C:/python/word-to-pdf/FitnessBuddy.pdf")

Running the above script will give you the expected output in the destination location.

Thanks for reading.

One Thought to “How to convert docx to pdf in Python”

  1. Suyash Vashishjtha

    Great man , thanks it really worked in seconds.. you are genius

Leave a Comment