Convert doc file to PDF in Python

In this blog, we will learn how we can convert .docx file into .pdf format using Python and its packages. It is quite a difficult task if we use general libraries and packages for the same but after a lot of research, I came across a custom package docx2pdf that once installed, makes the conversion very easy.

Installing the required package:

Enter the following command in the terminal and wait till the package is installed.

pip install docx2pdf

Conversion of the .docx file to .pdf format using Python

We now use the installed package to perform the conversion. The code looks something like this:

from docx2pdf import convert

convert("Name_Of_Your_Doc_File.docx")
convert("Name_Of_Your_Doc_File.docx", "Output.pdf")

Enter the name of your .docx file in place of the dummy name Name_Of_Your_Doc_File. Make sure both the .py file and the .docx file are in the same folder for avoiding conflicts.  This code will simply change the file format and create a new file in the same location and with the same contents but in .pdf format.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *