Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding features to audiobook #314

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added projects/Audiobook/Option 3.pdf
Binary file not shown.
40 changes: 31 additions & 9 deletions projects/Audiobook/main.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
import pyttsx3
import PyPDF2
from PyPDF2 import PdfReader
import threading
import keyboard # Import the keyboard library

pdf = None
stop_thread = False # Variable to signal stopping the playback

def play(pdfReader):
global pdf
global stop_thread

speaker = pyttsx3.init()

for page_num in range(pdfReader.numPages):
text = pdfReader.getPage(page_num).extractText()
for page_num in range(len(pdfReader.pages)):
if stop_thread:
break # Exit the loop if stop_thread is True
text = pdfReader.pages[page_num].extract_text()
speaker.say(text)
speaker.runAndWait()

speaker.stop()

def stop_playback():
global stop_thread
input("Press Enter to stop playback...")
stop_thread = True # Set the flag to stop playback

file = input("Enter your PDF file name : ") # Enter your own PDF file path
file = input("Enter your PDF file name: ")

while True:
try:
book = open(file, "rb")
pdf = PdfReader(file)
break

except Exception as e:
print("An error occured:\n", e)
print("\nEnter again\n")
print("An error occurred:\n", e)
print("\nEnter the file name again:\n")
file = input("Enter your PDF file name: ")

# Create a separate thread for playback
playback_thread = threading.Thread(target=play, args=(pdf,))
playback_thread.start()

# Start a thread for stopping playback with keyboard input
keyboard.add_hotkey('q', lambda: stop_playback())
keyboard.wait() # Wait for the hotkey event

pdfReader = PyPDF2.PdfFileReader(book)
# Wait for the playback to finish
playback_thread.join()