Type reader: pc speaks what you type

In this litlle script there is a little program to make the computer say the words you type. It uses gtts from google, so it saves the new words in a folder called audio.

gtts is an Api that uses the google translate tool to make the computer say what you type. So we will use this tool to make this program here. You will need to install it with pip install gtts or

# create the mp3 letters audio files
import os
from gtts import gTTS
import tkinter as tk
from pygame import mixer



def speak(word):
    print("looking for word in dictionary")
    print(os.listdir("audio/"))
    if f"{word}.mp3" not in os.listdir("audio/"):
        print("This is not present... creating")
        t = gTTS(word, "it")
        t.save(f"audio/{word}.mp3")
        print("created " + word)


def start():
    global word

    mixer.init()
    word = ""
    def audiokey(event):
        global word

        print(event.char)
        try:
            if event.char == " " or event.char in ",.!?":
                speak(word)
                mixer.music.load("audio/" + word + ".mp3")
                mixer.music.play()
                word = ""
            else:
                word += str(event.char)
                # mixer.music.load("audio/" + event.char + ".mp3")
                # mixer.music.play()
                print(word)
        except:
            word=""

    def indietro(event):
        global word
        
        print(event.char)
        word = word[:-1]
        print(word)
        try:
            mixer.music.load("indietro.mp3")
            mixer.music.play()
        except:
            pass

    def _return(evt):
        global word

        speak(word)
        mixer.music.load("audio/" + word + ".mp3")
        mixer.music.play()
        word = ""
 
    root = tk.Tk()
    root.title("Type to hear the pc reading the text when press space")
    root.geometry("600x400+200+400")
    text = tk.Text(root)
    text.pack()
    text.focus()
    text.bind("<Key>", audiokey)
    text.bind("<BackSpace>", indietro)
    text.bind("<Return>", _return)
    root.mainloop()


speak(word="spazio, indietro")
start()

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.