PDF Speaker using Python Only 8 line source code

 Step by Step Procedure to create PDF Speaker using Python

Hello friends how are you, Today in this post "PDF Speaker using Python" i am going to teach you how you can create a PdfSpeaker. I think it is something interesting for everyone because you have heard PdfReader but today i will teach using 8 lines of code to create PdfSpeaker. Here in this project upload any PDF file pass the page number and it will speak to you like a story telling, can't believe? just go through this post you will get something new today.

If you are a computer science students or teacher and want to learn something interesting in programming then just go through this post. This can also be a simple and interesting project for you.

We know that PDF[Portable Document File] file is the most used digital media file which is used to store many useful information for example subject notes, project reports , business related data, personal information etc. Whenever we need that data we open the file and we read that file but today i am going to present something new in which you can listen what you have stored into it. It is also said that  the thing like story , song etc which we listen we can remember for long time

Now  i am going to explain it step by step so just go through this post to understand this completely.

If you want to understand this through video then watch this video i have explained it step by step live

Step 1: Install Python : Click here to watch a video on how to install python or Open any browser and type Download Python and click the first link you will get official website of python here you will get a Download button and after clicking on this button you will get exe of latest python version just install it into your system. 

Step 2: Install Pycharm | Create Project | Install LibraryClick here to watch a single video on How to install Pycharm | Create Project | Install Library  or To install Pycharm IDE Open any browser and type Download Pycharm and click the first link you will get official website of Pycharm  here you will get a black download button and after clicking on this button you will get an exe of Pycharm , just install it into your system. If you are facing problem to install then watch the above video i have explained step by step.

Step 3: Create Project : Open Pycharm  click on File which is in top left of screen and select New Project. Then you will get a screen. Here first you need to select directory of your project and then type a name for your project like "PdfSpeaker" after directory name, and at last click the create button to create this project. 

Step 4: Create Python file: To create a python file for coding in your project just right click on project name "PdfSpeaker" and select new and click on Python File , you will get popup screen in which you have to type a name like "Program" for python file and press enter, it will create a python file with name Program.py inside the project PdfSpeaker. 

Step 5: Install libraries (pyttsx3 and PyPDF2) : Before doing code for this we have to first, install the libraries which are used to convert Text file into PDF file. To install this library just click on Terminal which will be in Left bottom of screen , you will get a console screen and in this console screen you have to type pip install translate and then press enter. After some seconds you will get a message like library is installed successfully. Still facing problem then click here to watch  video on How to install libraries.

Step 6: Store Pdf file  : Create any pdf file and write some sentences into it and store it inside the project folder and rename it with mypdf.pdf. Below is the content of mypdf file

PDF Speaker using Python

Step 7: Code ExplanationNow i am going to explain the code line by line

Line 1: import library: Here in this we will import the needed library 

import pyttsx3,PyPDF2

Here you can see that i have import two libraries in which one is pyttsx3 and another is PyPDF2. pyttsx3 stands for
py=Python
t=Text
t=To
s=Speech
x3=Version 3
and it will be used to speech the Text. Another library is PyPDF2 and it is used to Read PDF file.

Line 2: Open PDF file :Here in this line we will open a pdf file that we want to listen.
pdfFile=open("mypdf.pdf",'rb')

Here open() is a predefined function which is used to open file and here it is using to open a mypdf.pdf file which is stored inside the project folder. pdfFile is a variable in which now pdf file is sored.

Line 3: Read PDF file : Here in this step we will read the PDF file using PdfFileReader() function.
pdfReader=PyPDF2.PdfFileReader(pdfFile)

Line 4: Initialize Speaker: Here we will initialize the speaker.
speaker=pyttsx3.init()
Here init() is a predefined function of module pyttsx3 which is used to initialize the speaker.

Line 5: Read Page of PDF: Here we will read the content of particular page that we want to listen.
page=pdfReader.getPage(0)
Here getPage() is a predefined function which is used to get the content of particular page. Indexing of page starts from 0 that means if we pass 0 in its parameter like  getPage(0)  then it will read the content of first page.

Line 6: Get text of Page: Here we will extract the text of pdf page and store into a variable.
text=page.extractText()
Here extractText() is a predefined function which is used to extract the text of a page.

Line 7: Speech the Text: Here we will speech the obtained text.
speaker.say(text)
Here say() is a predefined function which is used to speech the text.

Line 8: Run and Wait Speaker: Here we will speech the obtained text.
speaker.runAndWait()
Here runAndWait() is a predefined function that make the speech Audible. If we forget to write this command then speech will not be audible.

Complete Python code:  Here is the complete code for this project/program you just need to type this code into your python file or you can copy this code for your personal use.

#import libraries
import pyttsx3,PyPDF2
#open open file
#Note: mypdf.pdf should be stored into project folder
pdfFile=open("mypdf.pdf",'rb')
#Read pdf file
pdfReader=PyPDF2.PdfFileReader(pdfFile)
#initialize speaker
speaker=pyttsx3.init()
#get particular page
#here 0 means page number 1
page=pdfReader.getPage(0)
#extract text of page
text=page.extractText()
#speech the text
speaker.say(text)
#make speech audible
speaker.runAndWait()

Just copy this code from here and paste it into program.py and to run this code right click on code area and click on Run Program you will get output like below


GUI Based Language Translator: Now i am going to give you something more interesting using this code . It will give you a GUI interface and where you can upload any pdf file and after uploading pdf file and can click on Play button to make your PDF audible.

Complete Code :Here is the complete code for PdfSpeaker

from tkinter import*
from tkinter import filedialog
def convert():
    # import libraries
    import pyttsx3, PyPDF2
    # open open file
    # Note: mypdf.pdf should be stored into project folder
    pdfFile = open(fname, 'rb')
    # Read pdf file
    pdfReader = PyPDF2.PdfFileReader(pdfFile)
    # initialize speaker
    speaker = pyttsx3.init()
    # get particular page
    # here 0 means page number 1
    page = pdfReader.getPage(0)
    # extract text of page
    text = page.extractText()
    # speech the text
    speaker.say(text)
    # make speech audible
    speaker.runAndWait()

def UploadAction():
    #uploading file
    filename = filedialog.askopenfilename(filetypes =[('Text Files', '*.pdf')])
    #showing message for uploaded file
    msgForUpload.set("Uploaded:"+filename)
    #declaring global variable
    global fname
    #passing uploaded filename into fname
    fname=filename


root =Tk()
#setting window height and width
root.geometry("600x200")
#setting window background color
root["bg"]="#424B4C"
#setting title of window
root.title("Krazy:PdfSpeaker")
#creating variable for messages
global msgForUpload
global msgForGenerate
msgForUpload=StringVar()
msgForGenerate=StringVar()
#label for uploading message
Label(root,text="",textvariable=msgForUpload,font="Arial 12",bg="#424B4C",fg="white").place(x=80,y=30)
#button for upload file
Button(root, text='Open', command=UploadAction,font="Arial 12 bold",width="10",bg="#0E2E1E",fg="white").place(x=80,y=70)
#button for convert
Button(root,text="Play",font="Arial 12 bold",width="10",command=convert,bg="#0E2E1E",fg="white").place(x=350,y=70)
#label for generated/success message
Label(root,text="",textvariable=msgForGenerate,font="Arial 12",bg="#424B4C",fg="white").place(x=80,y=120)
root.mainloop()

Here you just need to type this your code into program.py file or you can copy this code for your personal use only.

Step 8: Run Code: To run this code just right click on the coding area and click on Run Program and you will get a message output screen like below.

PDF Speaker using Python

Here you can upload any pdf by clicking on open button. After uploading of pdf file output screen will look like below.

PDF Speaker using Python

When you click on play button then you will get the audio of the text of page of PDF file. For live practical watch the above video.

I hope now you can  create "PDF Speaker using Python". If you have any doubt regarding this post  or you want something more in this post then let me know by comment below i will work on it definitely.
 

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

If you like my post then share it with your friends. Thanks😊Happy Coding.

Post a Comment

0 Comments