Hello there! 👋
In this article, I will teach you how to turn your PDF note into an audiobook with just a few lines of python code. This project is inspired by my younger sister, Lydia, who hates reading but loves listening to music.
Let's get started, shall we? 🚀Create and activate your virtual environment
audio_book is the name of my project directory
Replace name-of-virtual-environment with your desired name for your virtual environment
Once that is done. Start writing the python script 🚀
Install Text-to-speech python package
Pyttsx3 is a text-to-speech conversion library in Python. It is an easy-to-use tool that converts the entered text into speech. The pyttsx3 module supports two voices first is female and the second is male which is provided by “sapi5” for windows
Once you have installed the package import it into your .py file
Test the speech and see if it works
init stands for initialization.
.say() runs the output you want your script to say.
.runAndWait() makes your script stop talking after saying the code passed in the previous parameter.
For this to work effectively, you will need to add a PDF document in your project directory.
PyPDF is a pure-python library built as a toolkit for PDF. This package is capable of :
Extracting document information( such as author, book title, etc)
Splitting documents page by page,
Merging documents page by page,
Merging multiples pages into a single page,
Decrypting and encrypting PDF files.
Import PyPDF to open PDF document
Django-admin-cookbook.pdf is a PDF file that is in my project directory.
The open() function opens a file in text format by default. To open a file in binary format, add 'b' to the mode parameter. Hence the "rb" mode opens the file in binary format for reading.
.PdfFileReader() opens the PDF document in the book variable.
Pages: gets the number of pages the PDF has.
Read the PDF file
Speaker: initializes the pyttsx3 package.
setProperty: sets the rate of speech. You can change the value to your taste.
To get the full content of the PDF
This code gets the full text of the PDF.
Text: extracts the text from the PDF.
To save/download the full content of the PDF, we will need to add an empty string before the speaker variable and join it with the text variable, like this:
Download/Save your audio
myaudio.mp3: is the name of your audiobook.
You can change it to whatever you want.
That's it!🎉 You can now create your audiobooks and stop buying from amazon or kindle.
Now, you can read and sleep at the same time
I added two python ebooks to this project's GitHub repository. Have fun and thank you for the reading till the end❤️. Here is the Github link: github.com/Sophyia7/easy-read
I appreciate you!
If you appreciate what we do here on DigitalStade,
consider sending us an appreciation token;