Advertisement
_fedosssss_

Untitled

May 24th, 2024
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | Software | 0 0
  1. # Importing necessary libraries
  2. import PyPDF2
  3. import pyttsx3
  4.  
  5. # Prompt user for the PDF file name
  6. pdf_filename = input("Enter the PDF file name (including extension): ").strip()                        
  7.  
  8. # Open the PDF file
  9. try:
  10.     with open(pdf_filename, 'rb') as pdf_file:
  11.  
  12.         # Create a PdfFileReader object
  13.         pdf_reader = PyPDF2.PdfReader(pdf_file)
  14.  
  15.         # Get an engine instance for the speech synthesis
  16.         speak = pyttsx3.init()        
  17.  
  18.         # Iterate through each page and read the text
  19.         for page_num in range(len(pdf_reader.pages)):
  20.             page = pdf_reader.pages[page_num]
  21.             text = page.extract_text()
  22.             if text:
  23.                 speak.say(text)
  24.                 speak.runAndWait()      
  25.  
  26.         # Stop the speech engine
  27.         speak.stop()      
  28.         print("Audiobook creation completed.")
  29.  
  30. except FileNotFoundError:
  31.     print("The specified file was not found.")
  32.  
  33. except Exception as e:
  34.     print(f"An error occurred: {e}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement