Skip to content

Instantly share code, notes, and snippets.

@davit312
Last active January 3, 2024 11:20
Show Gist options
  • Save davit312/8fb79d8d45a684247f188f5a26dfd946 to your computer and use it in GitHub Desktop.
Save davit312/8fb79d8d45a684247f188f5a26dfd946 to your computer and use it in GitHub Desktop.
Windows text reader
import tkinter as tk
from tkinter import ttk
from threading import Thread
import subprocess
import time
import os
import pyclip
VOICES = {"en": "Microsoft Zira Desktop",
"ru": "Microsoft Irina Desktop",
"hy": "eSpeak-hy"}
def getLang(cptx):
am = 0
en = 0
ru = 0
count = 0
for i in cptx:
c = ord(i)
if c<63:
continue
elif c < 255:
en += 1
elif c<1300:
ru +=1
else:
am += 1
if count<64:
count +=1
else:
break
if am>ru and am>en:
return 'hy'
elif ru>en and ru>am:
return 'ru'
else :
return 'en'
END_OF_LOOP = False
def clipboard_listener():
previus_clip = pyclip.paste()
while True:
if END_OF_LOOP:
break
current_clip = pyclip.paste()
if previus_clip != current_clip:
lock = True
previus_clip = current_clip
os.system('balcon -k')
text = current_clip.decode('utf8')
voice_name = VOICES[getLang(text)]
text = text.replace("\n", " ")
text = text.replace("\"", "'")
print(f"start /MIN balcon -n \"{voice_name}\" -t \"{text}\"")
os.system(f"start /MIN balcon -n \"{voice_name}\" -t \"{text}\"")
time.sleep(3)
lock = False
time.sleep(0.9)
cp_watch = Thread(target=clipboard_listener, daemon=False)
cp_watch.start()
# root window
root = tk.Tk()
root.geometry('200x110')
root.resizable(True, True)
root.title('Reader')
# stop button
exit_button = ttk.Button(
root,
text='Stop TTS',
command=lambda: os.system('balcon -k')
)
exit_button.pack(
ipadx=50,
ipady=50,
expand=True
)
def window_exit():
global END_OF_LOOP
END_OF_LOOP = True
root.destroy()
os.system('balcon -k')
root.protocol("WM_DELETE_WINDOW", window_exit)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment