How to make an audio pdf reader using python PyPDF2

How to make an audio pdf reader using python PyPDF2

I have experimented on ways to make a self made text-to-speech pdf reader using Python and find PyPDF2, a python library to be the one who does the job.

Inspired by : geeksforgeeks.org/working-with-pdf-files-in..

Project requirements:

First, if you do not have the PyPDF2 and pyttsx3 installed, use pip install to install them.

Then, we import the required libraries and define the variables needed. We specify the directory or name of the pdf file we want to open in the open('bia-template.pdf' line.

import pyttsx3
import PyPDF2
book = open('bia-template.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(book)
pages = pdfReader.numPages
speaker = pyttsx3.init()

Then, we want the pdfreader to read according to user input. That means, we give a choice to user whether they want the reader to read all pages, read a certain number of pages or read until a specific page.

choice = input("Enter 1 if you would like to read until a specific page\nEnter 2 if you want to read all pages.\nEnter 3 if you want to specify page range.\n")
choice = int(choice)

Finally, we apply the if-else method in python for each one of the choices:

if choice == 1:
    newpage = input("Enter until what page do you want the audio to read\n")
    newpage=int(newpage)
    for num in range(0, newpage):
        page = pdfReader.getPage(num)
        text = page.extractText()
        speaker.say(text)
        speaker.runAndWait()

elif choice == 2:
    print(f'Reading all pages')
    for num in range(0, pages):
        page = pdfReader.getPage(num)
        text = page.extractText()
        speaker.say(text)
        speaker.runAndWait()

elif choice == 3:
    a,b=map(int,input("Specify page range seperated by space. Example,if from pages 2 to 12, enter: 2 12 \n").split())
    for num in range(a, b):
        page = pdfReader.getPage(num)
        text = page.extractText()
        speaker.say(text)
        speaker.runAndWait()

Below is the full code of the pdf reader:

import pyttsx3
import PyPDF2
book = open('bia-template.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(book)
pages = pdfReader.numPages

speaker = pyttsx3.init()
# print(pages)

# for 1st page only, put getpage(0)
# page = pdfReader.getPage(0)
# text = page.extractText()
# speaker.say(text)
# speaker.runAndWait()
choice = input("Enter 1 if you would like to read until a specific page\nEnter 2 if you want to read all pages.\nEnter 3 if you want to specify page range.\n")
choice = int(choice)



if choice == 1:
    newpage = input("Enter until what page do you want the audio to read\n")
    newpage=int(newpage)
    for num in range(0, newpage):
        page = pdfReader.getPage(num)
        text = page.extractText()
        speaker.say(text)
        speaker.runAndWait()

elif choice == 2:
    print(f'Reading all pages')
    for num in range(0, pages):
        page = pdfReader.getPage(num)
        text = page.extractText()
        speaker.say(text)
        speaker.runAndWait()

elif choice == 3:
    a,b=map(int,input("Specify page range seperated by space. Example,if from pages 2 to 12, enter: 2 12 \n").split())
    for num in range(a, b):
        page = pdfReader.getPage(num)
        text = page.extractText()
        speaker.say(text)
        speaker.runAndWait() 

        #https://www.codegrepper.com/codeexamples/delphi/input%28%29.split%28%29+in+python+3
# https://www.geeksforgeeks.org/working-with-pdf-files-in-python/

Now you can enjoy and have your pdf reader read for you.