Convert PDF File Text to Audio Speech using Python

File to Audio conversion reads the text aloud to the user. It helps in supporting learners who are struggling with print based information. It is helpful in proofreading, reading accuracy, understanding and in note taking. Let’s start developing this project which is very popular.

About Convert PDF File Text to Audio Speech Project

We will develop a Convert PDF file text to Audio speech project. The user needs to enter the starting page number and select the book he/she wants to read. After entering the details, the text of the selected book and specified page number will be read aloud.

Python project on converting PDF File Text to Audio Speech

The objective is to develop our own project on converting pdf file text to audio speech. Installation of tkinter is must before starting the project.

Project Prerequisites

The person should have sound knowledge of tkinter and python to begin the project.

Download Convert PDF File Text to Audio Speech Python Project

Please download source code of python pdf text to audio speech converter: PDF File Text to Audio Speech Converter

Project File Structure

1. Installation of tkinter
2. Importing Modules
3. Initializing Window
4. Creating Labels
5. File function

1. Installation of tkinter

Python gives multiple options for creating GUI. All Standard python distributions contain tkinter. Tkinter helps in creating Graphical user interface easily. Install tkinter before starting the project. Type the command mentioned below on your terminal window.

pip install tkinter

2. Importing Modules

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

Code Explanation:

a. filedialog:Unique dialogs that are used when dealing with files are provided with the help of this module
b. pyttsx3:This library converts text to speech
c. pyPDF2: Various functions related to pdf like merging, splitting, cropping and transformation are performed with this module.

3. Initializing Window

Window=Tk()
Window.geometry('500x350')
Window.config(bg="orange")
Window.title("Convert PDF File Text to Audio Speech Using Python by TechVidvan")

Code Explanation:

a. Tk(): All the components of the tkinter applications can be accessed with Tk().
b. geometry(): It is the deciding factor for length, height and width of the screen.
c. config(): Objects attributes are accessed after installation with the help of config.
d. title(): It sets the title of the tkinter screen that is specified in the parenthesis.

4. Creating Labels

startingpagenumber = Entry(Window)
page1.place(relx=0.02,rely=0.1)
startingpagenumber.place(relx=0.6,rely=0.1)
page1=Label(Window,text=”Enter starting page number”)
label = Label(Window, text="select a book.")
label.place(relx=0.3,rely=0.2)

Code Explanation:

startingpagenumber is a variable that stores the starting page number of the Selected book.

a. Entry(): It takes the single line input from the user.
b. place(): It places the widget on the tkinter screen.
c. relx: It is the distance from the x coordinate where the widget is to be placed.
d. rely:It is the distance from the y coordinate where the widget is to be placed.

5. File Function:

def file():
    path = filedialog.askopenfilename()
    book = open(path, 'rb')
    pdfreader = PyPDF2.PdfFileReader(book)
    pages = pdfreader.numPages
    speaker = pyttsx3.init()
    
    for i in range(int(startingpagenumber.get()), pages):
        page = pdfreader.getPage(i) 
        txt = page.extractText()
        speaker.say(txt)
        speaker.runAndWait()
 
B=Button(Window, text="Choose  the Book", command=file)
B.place(relx=0.4,rely=0.3)
 
mainloop()

Code Explanation:

a. askpoenfilename():It accepts the file name the user wants to open and opens that file.
b. PdfFileReader(): It helps in reading and viewing the pdf file.
c. extractText(): It extracts the text.
d. runAndWait(): It makes the speech audible to the user.
e. Button(): It adds a button on the tkinter screen.
f. mainloop(): It’s useful when the application is ready to run.

Python Pdf Text to Audio Speech Output

python pdf text to audio speech output

Summary

We have successfully created Convert PDF File Text to Audio Speech using python. We used tkinter and python while developing this project.