Reputation:
I want pyttsx to read text from my clipboard, so I can copy a text, and then let the computer read it for me, it works excellent, except its really annoying I can't stop the text to speech as I please, I either have to stop the whole program and restart or let it finish although I've lost interest.
I've tried using engine.startloop and engine.endloop but it just got my code stuck, maybe theres a way to use startloop in a way where I can exit the speech on a button press but I honestly cant figure out how to use startloop.
I've then tried this script:
import win32com.client as wincl
speak = wincl.Dispatch("SAPI.SpVoice")
speak.Speak("Hello World")
again. works great, but I don't know how to stop it mid speech.
oh, and yes I tried using threads, problem is engine.runAndWait() literally makes the speech run and everything else wait.
import keyboard
import win32clipboard
import pyttsx3
def init():
print("init")
def start(text):
engine = pyttsx3.init()
#more code here but its just setting the volume and speech rate
engine.say(text,"txt")
engine.runAndWait()
def action():
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
start(data)
keyboard.add_hotkey("ctrl+alt", lambda: action())
init()
so yeah basically, I'm looking for a way to stop text to speech with a hotkey, its fine if its not possible with pyttsx3 but I'd like to use a text to speech module that doesn't require internet as my connection is quiet poor at times haha
Upvotes: 3
Views: 8171
Reputation: 3455
There are a lot of ways to do that of course, but here is the simplest one I can think of, without having to use any external module, library, etc. You can use Python's built-in 'msvcrt' module to detect keyboard activity and stop if a any or a particular key is depressed:
import msvcrt as kb
import pyttsx3
engine = pyttsx3.init() # [This has to be done only once in a TTS session]
# Open the file in read mode
with open("file.txt",'r') as file:
for line in file:
if kb.kbhit(): exit() # You can follow it with 'kb.getch()' to check for
engine.say(line) # a particular key, etc.
engine.runAndWait()
Upvotes: 0
Reputation:
I feel really dumb right now, I solved it, I used pynput in my previous attempts, then tried with keyboard, got to the same point I was previously at (couldn't stop the text mid speech)
well anyway, if you are wondering about how to make pyttsx3 shut up with user input... here it is:
def onWord(name, location, length):
print ('word', name, location, length)
if keyboard.is_pressed("esc"):
engine.stop()
engine.connect('started-word', onWord)
just copy and paste that right above "engine.say('your text')" and the engine will stop the moment you press esc on your keyboard, also make sure to have:
import keyboard
at the top of your script
Upvotes: 3