Text-to-Speech Program Using Python

by Eli_H in Circuits > Raspberry Pi

13 Views, 0 Favorites, 0 Comments

Text-to-Speech Program Using Python

finale.png

I created a TTS program for school. In this picture you can see the original plans for this project. As I didnt complete this to the extent that I wished, but this is how far I got.

Supplies

Raspberry Pi 4

any small speakers

MicroSD card

Power Supply that does 5V and 3A

HDMI to MicroHDMI cable

monitor, mouse, keyboard

Physical Setup

PXL_20260506_165817711.jpg

Plug the speakers, mouse, keyboard, and monitor into the Pi4 and plug the Pi4 into the power supply.

Digital Setup

Download Python, go into Terminal and type:

sudo pip3 install gtts
sudo pip3 install playsound3

The Code

Im not going to do the code piece by piece as that makes it annoying to copy this since some code doesnt work without other parts and so on. So I will just put notes next to the code to say what it does.

#the library importing. The * symbol imports the whole thing.
from tkinter import *
from tkinter.ttk import *
from gtts import gTTS
from playsound3 import playsound

#converts text to speech
def make_speech():
tts = gTTS(txt.get())
tts.save(PATH)
playsound(PATH)

#will be used for storing speech.mp3
PATH = "C:\\Users\\Eli\\Documents\\speech.mp3" #Replace this path with your own

#makes the window object
root = Tk()
root.title("Text to Speech")
root.geometry("400x200")
root.config(bg='#bc5cdb')

#will store the speech as text
txt = StringVar()

#create a label
label = Label(root,
text ="Enter text:",
font= ('Helvetica 15 bold')).place(relx=0.5,rely=0.1,anchor=CENTER)
#create a textbox
speech_entry = Entry(root,
textvariable = txt,
font=('calibre',10,'normal')).place(relx=0.5,rely=0.3,anchor=CENTER)
#create a button
speak_btn = Button(root,text = 'Speak',
command = make_speech).place(relx=0.5,rely=0.5,anchor=CENTER)

#keep the window running
root.mainloop()

Yippee!

do.jpg

With that it should work. Theres a non-zero chance it doesnt work, but oh well. This is my first Instructable, so I dont know if I can update these things, but if I cant then I will be making a new one where I either make it not need the monitor and the mouse, or I make it use a Vocaloid for a voicebank. But for you, you are done! hooray