Tuesday, 21 May 2024

Pdf To Audio using Python

 


# Importing necessary libraries

import PyPDF2

import pyttsx3

# Prompt user for the PDF file name

pdf_filename = input("Enter the PDF file name (including extension): ").strip()

# Open the PDF file

try:

    with open(pdf_filename, 'rb') as pdf_file:

        # Create a PdfFileReader object

        pdf_reader = PyPDF2.PdfReader(pdf_file)

        

        # Get an engine instance for the speech synthesis

        speak = pyttsx3.init()        

        # Iterate through each page and read the text

        for page_num in range(len(pdf_reader.pages)):

            page = pdf_reader.pages[page_num]

            text = page.extract_text()

            if text:

                speak.say(text)

                speak.runAndWait()       

        # Stop the speech engine

        speak.stop()      

        print("Audiobook creation completed.")

except FileNotFoundError:

    print("The specified file was not found.")

except Exception as e:

    print(f"An error occurred: {e}")

#clcoding.com


Explanation:

This script segment accomplishes several tasks:

Importing Necessary Libraries:

PyPDF2: This library is used to work with PDF files, allowing us to read the content of PDF documents.
pyttsx3: This library is used for text-to-speech conversion, enabling us to convert the text extracted from the PDF into spoken words.
Prompting User for PDF File Name:

The input() function is used to prompt the user to enter the name of the PDF file, including its extension (e.g., example.pdf).
The entered file name is stored in the variable pdf_filename.
Opening the PDF File:

The open() function is used to open the PDF file specified by the user. The file is opened in binary read mode ('rb').
This operation is wrapped in a try block to handle possible exceptions, such as the file not being found (FileNotFoundError) or other unexpected errors (Exception).
Reading PDF Content and Converting to Speech:

If the PDF file is successfully opened, a PdfReader object named pdf_reader is created using PyPDF2. This object is used to read the content of the PDF document.
An instance of the text-to-speech engine (speak) is initialized using pyttsx3.init().
The script iterates through each page of the PDF using a for loop and the range(len(pdf_reader.pages)) construct. For each page:
The text content is extracted from the page using page.extract_text().
If the extracted text is not empty, it is passed to the text-to-speech engine to be spoken aloud using speak.say(text) and speak.runAndWait().
After processing all pages, the text-to-speech engine is stopped using speak.stop().
Error Handling:

If the specified PDF file is not found (FileNotFoundError), the script prints a message indicating that the file was not found.
If any other unexpected error occurs during the execution of the script (Exception), the error message is printed.
Completion Message:

If the script executes successfully without encountering any errors, a message indicating the completion of audiobook creation is printed.
Overall, this script segment enables the user to specify a PDF file, reads its content, converts the text to speech, and creates an audiobook from the PDF content.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (53) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (932) Python Coding Challenge (364) Python Quiz (25) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses