Python Audiobook Project

Himani Bansal
Wiki Flood
Published in
3 min readNov 21, 2023

About Python Audiobook Project:

In this Python Audiobook Project, we will build an Audiobook application in Python. This application reads the text of the pdf file which we provide.

Prerequisites For Python Audiobook Project:

  • First, we’ll initiate the project by installing the essential libraries and modules via the pip installer.
  • A strong grasp of Python, coupled with proficiency in the required libraries and modules, is crucial for the successful development of this project.
pip install tkinter
pip install PyPDF2
pip install pyttsx3
Python Audiobook Project
Python Audiobook Project

Importing the required libraries and modules in our program:

from tkinter import *
import PyPDF2
import pyttsx3
from tkinter import filedialog

tkinter- Used for creating the graphical user interface (GUI).

PyPDF2- Used for reading PDF files.

pyttsx3- Used for text-to-speech conversion.

filedialog- Used for opening file dialogues to browse for a PDF file.

Initialising the GUI Window:

root = Tk()
root.geometry('400x180')
root.configure(bg='green')
root.title("PythonFlood-AudioBook")
root.config(bg="#D3D3D3")

root- It is the name of our GUI window.

Tk()- It initialises tkinter which means a GUI window is created.

geometry()- This method provides the length and breadth to the GUI window.

resizeable()- This method allows the window to change its size as per user need.

title()- This method gives title to the window

confg()- This method sets the configuration of the window.

Creating Label and Button widgets- We will create Label and Button widgets.

Label(root, text="AUDIOBOOK", font=("Arial", 18), bg='#8B8B7A', fg='White', width=30).pack(pady=10)
pathlabel = Label(root, width=50)
pathlabel.pack()


Button(root, text=("Browse Book"), font=('Arial', 12), bg='#8B8B7A', fg='White', command=browse).pack(pady=10)
Button(root, text=("Audio"), font=('Arial', 12), bg='#8B8B7A', fg='White', padx=10, command=save).pack()


root.mainloop()

Label()- It displays one line or more than one line of text.

text- It is used to display text on label.

font- It is a style in which font is written.

bg- It is the background Colour of the label.

pack()- This displays the widgets in the GUI window.

place()- It is used to set the position.

Code for browsing the PDF File- We will create a function for browsing the file.

def browse():
global pdfReader
file = filedialog.askopenfilename(title="Select a PDF", filetypes=(("PDF Files", "*.pdf"), ("All Files", "*.*")))
pdfReader = PyPDF2.PdfReader(open(file, 'rb'))
pathlabel.config(text=file)

browse()- This function is called when the “Browse Book” button is clicked.

filedialog.askopenfilename()- Opens a file dialog for selecting a PDF file.

PyPDF2.PdfReader()- Reads the selected PDF file.

pathlabel.config()- Updates a label to display the selected file’s path.

Code for main function- Here, we will create the main function of the application.

def save():
global speaker
speaker = pyttsx3.init()


for page_num in range(len(pdfReader.pages)):
text = pdfReader.pages[page_num].extract_text()
speaker.say(text)
speaker.runAndWait()
speaker.stop()

pyttsx3.init()- It is used to create an object.

pdfReader.pages[]- Is is used to retrieve all the pages of pdf.

speaker.say()- It is used to convert the text into speech.

speaker.runAndWait()- This method waits until the speech is completed.

speaker.stop()- It is used to stop the speech process.

Python Audiobook Project Output-

Audiobook in Python Output
Audiobook in Python Output

Conclusion

We have successfully developed the Audiobook application in Python. This application allows users to convert text-based PDFs into audio, offering an accessible and convenient way to consume written content. Users can simply browse for a PDF, and the application will read it aloud, making it a useful tool for those who prefer audio content or have visual impairments.

--

--

Himani Bansal
Wiki Flood

Doing my Best to Explain Data Science (Data Scientist, technology freak & Blogger)