How to Convert a PDF into an Audio
You are having a lot of PDFs that you should read, but you don’t have time? so this solution is in this article. You can convert any PDFs into audio and you listen to it while doing something else.
The original article on my blog website here.
As a student, scientist, or anyone who likes to read. We read PDFs almost every day. It can be a story, book, a paper…
But sometimes, we get bored from reading all the PDF especially the lazy people like me. For that, we prefer to listen to that PDF instead of reading it.
By doing this, we will make life easier, so that you can convert a PDF into an audio and you can listen to it while doing sport or something.
In this small article, I will show you how you can do that using few lines of code using Python.
To do this work, there are only two steps that you should do, the first one is to extract the text from the PDF and put it as a string, then you convert that string into audio using simple libraries.
Extracting the text
To do this step, we need to install a library called ‘PyPDF2’ which can read any PDF and convert it into a string. So to install this library, you can use the terminal by typing this line:
pip install pypdf2
Now the library is installed, to use it you can see that it is easy as well. You need to read the pdf
You can see that we used the function ‘open’ which is a python function by default that will open the file as binary (because we specified that in the parameters ‘rb’ for read binary).
The next step is that we should specify the page that we want to extract the text from by using the following function:
And finally, to extract the text from the chosen page, we need this line of code:
Converting the text into audio
Now after that you saw how to extract the text from the PDF, I will show you how to convert this text into an audion in few steps.
Starting by importing the pyttsx3 library then, we need to initialize the speaker (line 3). After doing this you can just call the function ‘say’ which will take a string in its parameters then convert it into audio.
Finally, you have to run the speaker by calling the function ‘runAndWait’ (line 6).
And congrats, when you run the code the text on the page selected will be converted into audio.
If you want to convert the whole PDF on one run, then you can create a loop that loops on all the pages of the PDF.
If you want to see the tutorial about this blog, you can see this playlist that I put on my YouTube channel.