Hey welcome back, so today we are going to do something to automate our reading task using Python. We are going to build a GUI program to select pdf files and then play them inside our software, exactly no more eye reading it’ll read it for you and all you need to do is to sit back and enjoy.
- Happy relationship with basic Python and
yeah that’s it, I’ll be explaining the rest 😉.
So now we know what we are going to do so let’s break it down into smaller chunks and focus on each of them individually.
First of all we are going to create a window and a dialog box to open the desired pdf file. We also create a text box for displaying the pdf text content and a button
play for start playing it as audio.
tkinter(for dealing with GUI)
gTTS(for converting text into speech)
playsound(for playing the audio file)
PyMuPDF(for reading pdf files)
before moving ahead I want to tell you something that in most of the online tutorials you’ll find people using
PyPDF2 for working with pdf files but the reason we are not using it is because it does not always work, like till the date I'm writing this post if you use
PyPDF2 for reading pdf generated by google like from google docs, it's not able to read text from it.
gTTS stands for Google Text To Speech it's a Python library as well as a CLI tool for converting text into speech.
playsound is also a Python library for playing audio files like
we are using
playsoundjust for playing the audio file that will be created using
gTTS, you can use any Python library for that like
osmodule to play on native audio player installed on terminal, but I guess this only works on
MAC OS Xand
Let’s dive into the code now ✨
Step 1 :
In this step we’ll be creating our GUI so open up your favourite code editor and create a file as
main.py and import
tkintercomes preinstalled with Python so no need to install it from
Now if you run it, you’ll see something like this,
Step 2 :
In this step we will create function
open_pdf this function will create a dialogue box for selecting pdf file and then reading all of it text and showing inside the created earlier, then it'll use
gTTS for creating audio file of all the text from .
You need to install
PyMuPDF, so inside your terminal run
pip install PyMuPDFand
pip install gTTSfor installing them.
As you can see above code is self explanatory but still I want to highlight some points. First look at the line that says
text_box.insert(END, content) basically
END is defined inside
tkinter and it returns the last index that means where is the end of file, similarly
1.0 means the beginning of the text.
So basically when we store the first page data inside text box then
starting index == last index == END after that we'll keep inserting text at the end of the previous stored text.
Step 3 :
Now we have the function so it’s time to provide each widget it’s own functionality like pressing button and clicking on menu really perform something.
Go to the code and add
command attribute to all the
file_menu dropdowns and
play_btn as show below
from playsound import playsoundfile_menu.add_command(label="Open", command=open_pdf)
file_menu.add_command(label="clear", command=lambda: text_box.delete(1.0, END))
file_menu.add_command(label="Exit", command=window.quit)play_btn = Button(text="Play", command=lambda: playsound("audio.mp3"))
pyobjcas dependency for working so you need to install it by
pip install pyobjc
Basically function provided in
command will execute as you click on the widget. For short function like
exit we used
window.quit will close the window and
clear is self explanatory. As the gets saved
playsound("audio.mp3") will play it after you click the button.
So if you followed well then in the end your final code will somewhat look like:
Let’s test it
Now it’s time to run our code and check if everything is working or not, take a sample pdf file with some text and open it.
YAYYYYYY…… 🎉 🥳, WE DID IT GUYS
we just created our own pdf to audio book convertor, now if you want to go some steps further I will recommend you to read gTTS official documentation also if someone wants then you can convert this python script into
exe file and share it with your friends so they can have fun too
converting python scripts to .exe filesfor next tutorial 😅.
What’s next !
If you are still reading, make sure to follow me on Twitter as I share some cool projects and updates there and yeah don’t forget I have some exciting stuff coming up every weekend. See Y’all next time and stay safe ^^ 🌻
Originally published at https://swayam-blog.hashnode.dev.