MAKING A PDF READER WITH PYTHON BY @abdulkahargunu

in Account Booster 👍3 years ago

Hello! I am Abdul Kahar Gunu, new in this community. I am into many things and programming is one of them, and today I would to share with you something cool with python programming language.


How would it be if you could just put your lecture notes on play, book or any PDF book at all on play? You would sit lazily and enjoy it right😊.
Here’s how a simple PDF reader is made using python with less than 20 lines of code.


Using the IDE, you prefer

  1. Create a new python project and call it a name you prefer, in my case it’s “Reader”
  2. Create a new python file in the project you just created and call it a name you prefer, in my case it’s “reader.py”
  3. Type and run "pip install pyttsx3”, in your IDE terminal and wait for it install.
  4. Type and run "pip install PyPDF2”, in your IDE terminal and wait for it install.
  5. Copy and paste the PDF you wish to read in the project folder (The project you just created).
  6. Copy and paste the code bellow in the python file you created.

import pyttsx3
import PyPDF2
#Referencing the PDF to read.
bookNmame = input ('Please enter the name of the pdf Document you want to read, make sure to add the ".pdf" extension: ')
BookToRead = open(bookNmame,'rb')

PDFreader = PyPDF2.PdfFileReader(BookToRead)

NumberOfPages = PDFreader.numPages

Speaker = pyttsx3.init()
# Prints the number of pages in the PDF document 
print(f'There are a total of {NumberOfPages} pages in {bookNmame}')


#Gets the page to start reading from the page number that will be inputted
WhereToStart = int(input('Which page do you wish to start from : '))
StartFrom = int(WhereToStart - 1)
pagetostart = PDFreader.getPage(StartFrom)

#Extracts the text
text = pagetostart.extractText()
#Reads out the Extracted Text
Speaker.say(text)
Speaker.runAndWait()

After completion, your project folder should be similar if not the same as mine bellow.

pic 1.JPG


After pasting the code, you should have something like this bellow

pic 3.JPG


Explanation of the code above


Line 1: Imports pyttsx3 (Text to Speech 3 libraries).
Line 2: Imports PyPDF2 (the libraries we need for the manipulation of PDF’s).
Line 5: Asks the user to input the name of PDF file to read from.
Line 6: Opens the book referenced and makes sure it’s a PDF file.
Line 8: Loads the PDF file referenced at line 6.
Line 10: Gets the number of pages present in the PDF file.
Line 12: Initializes the speaker.
Line 14: Prints the number of pages in the PDF file referenced.
Line 17: Asks the user to input the page he/she wishes to start from.
Line 18: Gets the actual page the user wishes to start from.
Line 19: Sets where the program will start reading from with the actual page number.
Line 22: Extracts the text to be read out to the user.
Line 24: Lets the program start to speak.
Line 25: Lets the program speak in sequences.


Have fun with your simple PDF reader.
If you find this interesting please leave a comment bellow, If you know anyone who may find this interesting or helpful please resteem.

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63178.17
ETH 2581.50
USDT 1.00
SBD 2.71