An audiobook is a recording or voiceover of a book or other work read aloud. You can listen to audiobooks on any smartphone, tablet, computer, home speaker system, or in-car entertainment system. In this article, I will walk you through how to create an audiobook with Python.
You don’t need to buy a subscription for an audiobook if you have a pdf of the book. At the end of this article, you will learn how to create an audiobook with the python programming language in a few lines of code.
Let’s Create an Audiobook with Python
Python has a large collection of libraries due to the very active community which serves various purposes. Here we need to use two libraries pyttsx3 and PyPDF2 to create an audiobook with Python.
Both the above libraries can be easily installed by using the pip command; pip install PyPDF2, pip install pyttsx3.
Reading the PDF File
PyPDF2 allows manipulation of pdf in memory. This python library is capable of tasks such as:
- extract information about the document, such as title, author, etc.
- document division by page
- merge documents per page
- cropping pages
- merge multiple pages into one page
- encrypt and decrypt PDF files
- and more.
I will use this library to split the pdf file page by page, then read the text on each page, then send the text to the next step in the process to create an audiobook with Python.
import PyPDF2 pdfReader = PyPDF2.PdfFileReader(open('file.pdf', 'rb'))Code language: Python (python)
The pyttsx3 library is capable of converting text to speech offline. The text that we read from a pdf is then fed as an input to the text-to-speech engine:
import pyttsx3 speaker = pyttsx3.init()Code language: Python (python)
Now the next step in the process is to loop the process for each page of the pdf file and stop the pyttsx3 speaker engine last:
for page_num in range(pdfReader.numPages): text = pdfReader.getPage(page_num).extractText() speaker.say(text) speaker.runAndWait() speaker.stop()Code language: Python (python)
Now the next step is to save the audio as mp3 file:
engine.save_to_file(text, 'audio.mp3') engine.runAndWait()Code language: Python (python)
This is how we can build an audiobook with Python in a few lines of code. I hope you liked this article on how to create an audiobook with the python programming language. Feel free to ask your valuable questions in the comments section below.