Word to PDF Converter App using Python

Prajwal Mani
Analytics Vidhya
Published in
4 min readMar 1, 2021

Hey, hope you are having a fantastic day!

We have converted so many docs files to Pdf with the help of some third-party apps like smallpdf.com or ilovepdf.com. I thought why not just build our own application with the help of python robust libraries.

The module we will be using for this is

  • tkinter: A python GUI toolkit that comes as a built-in module with python which means we don’t have to install using the pip command
  • docx2pdf: A python module that helps us to convert word to pdf

To install docx2pdf

pip install docx2pdf

You can use any IDE which you like but don’t forget to turn on the dark mode to save your eyes!

Now we have all the modules we need

Let’s import the modules

import tkinter as tk
import tkinter.ttk as ttk
from tkinter.filedialog import askopenfile
from tkinter.messagebox import showinfo
from docx2pdf import convert
  • tkinter: our GUI python module
  • tkinter.tkk: To access the widget set of the tkinter module
  • tkinter.filedialog: To create a simple dialog box
  • tkinter.messagebox: To create a message box

We will set up a tkinter window object and the title for our window

win=tk.Tk()
win.title("Word to Pdf Converter App")

Let’s handle some basic file operations like

  • Accepting only word files as our input
  • Converting that into pdf
  • Saving the pdf file

All those 3 steps we will define in a single function

def openfile():
file = askopenfile(filetypes=[('Word Files', '*.docx')])
convert(
file.name,
r'C:\Users\prajw\OneDrive\Desktop\doc2pdf\doc2pdfconverted.pdf'
)
showinfo("Done", "File successfully converted ")

To open the File Explorer interface we use askopenfile() from tkinter. filedialog and pass a parameter filetypes which is a list where we specify files of type and all the file extensions we want to accept as our inputs now for this particular instances we just need a word file that is with .docx extension

* before “.docx” tells the file explorer that the file name can be anything but it should be a doc file

We got the file, now let us convert it to pdf and save it

With the help of the docx2pdf module, we have convert() which will do our job we have to pass two parameters for it

  1. File Name: file.name(The name of the file we selected using askopenfile())
  2. File path: Here we are specifying the location where our converted file should be saved. You can give any file path you need but don’t forget the “r” it just tells python to treat the file path as a raw string

After everything, we will just pop a success dialog box telling that the file was successfully converted with showinfo()

We just completed the logic part now let’s create a simple GUI

Here we will just add the components to the main window object we created before

We will add some text and a button

label=tk.Label(win,text="Choose a file!")
label.grid(row=10,column=5,padx=5,pady=5)

For tk.Label we have 2 parameters, one is the window object and the other is the text we want to display

The grid() acts as a widgets organizer with a table-like structure on the parent widget

The grid parameters we will be using is

  • row and column: This allows us to place the object in our desire place. For eg: If we want to place our text in the second row and 3 columns we should initialize values like this, rows=2 and column=3
  • padx and pady: For padding around the widget

Next, we will add a button that will call our openfile()

button=ttk.Button(win,text="Select",width=30,command=openfile())
button.grid(row=20,column=5,padx=5,pady=5)

Button() has 4 parameters two are similar to that of the label component the new ones are the width and command

  • width: which tells how much width the button should be
  • command: After the button we want it to perform some task, our task for the button to perform is to call the openfile()

The next line is the grid we already know about that. Damn, we are quick learners!!

And the last line is..

win.mainloop()

This line communicates python to run tkinter event loop so that the methods listen to the events like button pressed or mouse dragged and many more events

Now let us see some snapshots

The main window

Let’s select the file

Success message dialog

Checking whether the file is converted and saved in our file explorer

Our file is safe and sound!!

So that’s the end folks we just created a Word to Pdf converter App in just 16 lines with Python

Hope you learned something from this article and thank you for dedicating a few mins of your day to it.

If you have any doubts just comment down below, I will be happy to help you out!

Thank you!

-Mani

--

--

Prajwal Mani
Analytics Vidhya

Just a random kid who is interested in ML|DL|DS|CS Student.books, poems and singing are my weakness, check me out at :https://linktr.ee/prajwal.mani