[go: up one dir, main page]

0% found this document useful (0 votes)
14 views8 pages

Python Project1

Uploaded by

shahnishi71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

Python Project1

Uploaded by

shahnishi71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Abstraction

In this project first of all we created an audiobook which means :

providing an auditory version of a written text that can be listened to instead of read.

Here we used the following modules:

i. PyPDF2: Python library used to read and extract text from PDF files.
ii. Pyttsx3: Python library used for text-to-speech conversion And many more…

In this we provided 3 pdf files , giving the user option to choose any of this three pdfs .

Table of content:

SR NO. CONTENT

1 ABSTRACTION

2 INTRODUCTION

3 EXPLANATION

4 CODE AND OUTPUT


Introduction
A Python audiobook is a program that automatically converts text, typically from a PDF or
other text-based document, into speech using Python's text-to-speech capabilities. By
combining libraries such as PyPDF2 for extracting text from PDF files and pyttsx3 for
converting text into spoken words, Python allows for the creation of simple audiobooks.

A Python audiobook program is a simple yet powerful tool to enhance accessibility and provide
a convenient way to "read" content through listening.

Here, we have provided user three different options of pdfs to choose from as the user likes.
After the user chooses the favorable option, the interpreter will ask them to enter the page
number from which they want start reading and the page number to which they want to read
up to.

After completion of this, the interpreter will give them the options along with ‘close’ option to
terminate the program.

The following are the further details about the modules and commands that we used in our
project:
Explanation:
Step 1:
First of all we install pyttsx3 from pip command it is Python library used for text-to-speech
conversion in our project.

Step2:

Python library used to read and extract text from PDF files in project .
Step 3:
Now we extract the pdf files from online pdfs and save them in desktop

In this screen you can see that 3 files exp, Rdbms, flower this 3 pdf files we take from
desktop and give their pdf path to our project to run the pdf like in our project pdf text covert
into audio.
Step 4:
As in picture shows like we select the path and copy the path and past the path as located as
so on for others..
Step 5:
Now for run the code 1st select to run the 1st file
2nd for 2nd file
3rd for 3rd file and close for closing the program
Code and Output
import PyPDF2
import pyttsx3

# Function to convert PDF to Audio


def pdf_to_audio(pdf_file, start_page=0, end_page=None):
try:
# Initialize PDF reader
with open(pdf_file, 'rb') as file:
reader = PyPDF2.PdfReader(file)
if end_page is None or end_page > len(reader.pages):
end_page = len(reader.pages)

# Initialize text-to-speech engine


speaker = pyttsx3.init()

# Loop through the specified pages


for page_num in range(start_page, end_page):
page = reader.pages[page_num]

text = page.extract_text()

# Check if text was extracted properly


if text:
print(f"Reading page {page_num + 1}")
speaker.say(text)

speaker.runAndWait()
else:
print(f"No text found on page {page_num + 1}")

except FileNotFoundError:
print(f"Error: The file '{pdf_file}' was not found.")
except Exception as e:
print(f"An error occurred: {e}")

# Define paths for the three PDFs


pdf_paths = {
1: r"C:\Users\Nishi\OneDrive\Desktop\Rdbms.pdf",
2: r"C:\Users\Nishi\OneDrive\Desktop\Rdbms.pdf",
3: r"C:\Users\Nishi\OneDrive\Desktop\rd.pdf"
}

def choose_pdf():
while True:
# Ask the user which PDF they want to read
choice = input("Enter 1, 2, or 3 to choose a PDF file, or type 'close' to exit: ")

if choice == 'close':
print("Closing the program.")
break
elif choice in ['1', '2', '3']:
pdf_choice = int(choice)
print(f"You have chosen PDF {pdf_choice}.")
start_page = int(input("Enter the start page (default is 0): ") or 0)
end_page = int(input("Enter the end page (default is all): ") or None)
pdf_to_audio(pdf_paths[pdf_choice], start_page, end_page)
else:
print("Invalid input. Please choose 1, 2, or 3, or type 'close' to exit.")

# Start the program


choose_pdf()

Output:
Conclusion:
This Python audiobook script provides a simple yet effective way to convert PDF content into
speech. By leveraging the PyPDF2 library for text extraction and pyttsx3 for text-to-speech
conversion, the program allows users to listen to their PDF files as audiobooks. The script
offers flexibility by allowing users to select specific PDFs and page ranges, while handling
errors gracefully to ensure a smooth user experience.
The program is ideal for those seeking a lightweight solution for converting PDFs into an
accessible and engaging audio format, enhancing convenience and accessibility for a wide
range of users.

You might also like