Skip to content

Instantly share code, notes, and snippets.

@willwade
Last active July 20, 2022 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willwade/92d4000e646416aaee4d90460de3b539 to your computer and use it in GitHub Desktop.
Save willwade/92d4000e646416aaee4d90460de3b539 to your computer and use it in GitHub Desktop.
TTSfromClipboard.py. A quick hacky go at making a script from reading the pasteboard. Its not great -and you cant change voices unless you hard coded it. And we'd need to do some funky stuff like NLTK to find last para or last sentence.
from pynput import keyboard
import pyperclip
import pyttsx3
converter = pyttsx3.init()
# Sets speed percent
# Can be more than 100
converter.setProperty('rate', 100)
# Set volume 0-1
converter.setProperty('volume', 1)
listenAllTime = False
def on_activate_sayall():
data = pyperclip.paste()
converter.say(data)
converter.runAndWait()
def on_activate_saylast_sentence():
data = pyperclip.paste()
converter.say(data)
converter.runAndWait()
def for_canonical(f):
return lambda k: f(l.canonical(k))
if listenAllTime:
last_data = None
while True:
# get clipboard data
data = pyperclip.paste()
if data != last_data:
# Changed clipboard
converter.say(data)
converter.runAndWait()
last_data = data
else:
hotkey = keyboard.HotKey(
keyboard.HotKey.parse('<ctrl>+<alt>+s'),
on_activate_sayall)
with keyboard.Listener(
on_press=for_canonical(hotkey.press),
on_release=for_canonical(hotkey.release)) as l:
l.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment