Course Content
What is Python?
Introduction of Python and Its setup
0/2
Control Statement
Control statements are used to control the flow of execution depending upon the specified condition/logic.
0/4
File Handling
File handling is an important component of any application. Python has multiple functions for creating, reading, updating, and deleting files.
0/2
Examples
Following are the examples of python scripts to try hands-on, you are ready to start your python journey.
0/7
Python
About Lesson

Overview

In this tutorial, you will learn how to an audiobook from a given PDF in Python. We will be using the PyPDF2 module for extracting the text from PDF files. pyttsx3 module for text to speech conversion.

Installing the module

To install the PyPDF2 and pyttsx3 module and some other related dependencies, we can use the pip command:

pip install pypdf2

pip install pyttsx3

Explanation

1)Import all the libraries

2)Create a PDF file object

3)Create a PDF reader object

4)Print number of pages in PDF file

5)Convert Text to speech using pyttsx3

6)Count the number of pages and run the loop till the page you want to read.

The Code

import pyttsx3
import PyPDF2

book = open('sample.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(book)
pages = pdfReader.numPages
print(f"Total Pages: {pages}")
print("nn")
speaker = pyttsx3.init()

for num in range(1, pages):
    page = pdfReader.getPage(num)
    text = page.extractText()
    
    print(f"Page: {num}n")
    print(text)
    print("nn")
    speaker.say(text)
    speaker.runAndWait()