3 Methods to convert Docx files into PDF files using Python

Rahul Bhatt
Nov 21 · 3 min read
Image for post
Image for post

Have you always wanted to convert Docx files into pdf in a batch? If yes, these python scripts will make your life much more comfortable.
The two methods of which one includes GUI another doesn’t. The following one is slightly different and requires a Word application installed on your system.To install docx2pdf:

The first one will be without GUI:

pip install docx2pdf

Image for post
Image for post
Fig.1. Python Code

Explanation: The convert() function takes two arguments that are the abs path of the file you want to convert and where you want to save. You can do batch convert by providing the folder path.

This library also allows using CLI instead of writing a code in a separate file.

Image for post
Image for post
Fig.2. CLI commands

To implement this using GUI, you must install PyQt5:

pip install PyQt5

Image for post
Image for post
Fig.3. Drag drop ft. to convert docx to pdf in batch

Output:

Image for post
Image for post
Fig.4. Main window where user has to drag and drop files
Image for post
Image for post
Fig.5. Processing window
Image for post
Image for post
Fig.6. Docx files get converted into PDF

Explanation: There is a lot of theory to cover all the functions used, but we will only see the important ones. You can get more information by checking out the references attached below.

QMimeData belongs to the QtCore module stores data on a clipboard then is used in the drag and drop process.

DragEnterEvent: provides an event sent to the target widget as dragging action enters it.

DragMoveEvent: is used when the drag and drop action is in progress.

DropEvent: is the event that occurs when the drop gets completed.

hasUrls(): Returns true if the object can return a list of URLs; else returns false.

The basic idea is to use PyQt5 to create a GUI, PyQt.QtWidgets to use QListWidget and its functions and PyQt5.QtCore to get the URI-list of the files, basically the location of the file, and then converting the file at this location.

There is also another way to convert Docx files into PDF that requires a word application installed on your system.

Install this first:

pip install comtypes

Image for post
Image for post
Fig.7. Another way to convert Docx file into PDF

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Sign up for Analytics Vidhya News Bytes

By Analytics Vidhya

Latest news from Analytics Vidhya on our Hackathons and some of our best articles! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Rahul Bhatt

Written by

I m a passionate coder who occasionally share the joy of writing. I write articles about programming, gaming, and psychology. Thank you for following :)

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Rahul Bhatt

Written by

I m a passionate coder who occasionally share the joy of writing. I write articles about programming, gaming, and psychology. Thank you for following :)

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store