Skip to content

Commit

Permalink
Merge pull request #314 from ayan-joshi/Adding-features-to-Audiobook
Browse files Browse the repository at this point in the history
Adding features to audiobook
  • Loading branch information
Mrinank-Bhowmick authored Sep 22, 2023
2 parents 265d66f + 4cb310a commit 6daa97c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
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()

0 comments on commit 6daa97c

Please sign in to comment.