-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_to_audio.py
More file actions
28 lines (27 loc) · 763 Bytes
/
pdf_to_audio.py
File metadata and controls
28 lines (27 loc) · 763 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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