Python gtts module: How to Convert Text to Speech in Python?

Text To Speech In Python

Hello Fellow learner! Today we are going to build our very own text to speech convertor!

Let’s Begin!

Introduction to the Project

Text to speech implies translating the text into human understandable speech. The application can be very useful for people who have trouble reading sentences and words properly.

In the application, the user enters some text into the entry box and then the application turns the text into audio with just a single click on the button.

1. Importing Libraries

Let’s start off by importing the libraries required for the application which are as follows:

  1. tkinter
  2. gTTS,
  3. playsound
from tkinter import *
from gtts import gTTS
from playsound import playsound

2. Creating Initial Tkinter Window

First we initialized tkinter window and add geometry and other configurations to the window including the background color and title.

window = Tk()
window.geometry("350x300") 
window.configure(bg='#FAD7A0')
window.title("TEXT TO SPEECH")

3. Adding Widgets to the Window

The next step involves adding labels, entry boxes, and buttons on the screen. The code for the same is shown below. The widgets declarations are highlighted for your convenience.

For the application we would be using three buttons. One to play the text, second is to reset the application and the last one is to exit the application.

Label(window, text = "        TEXT TO SPEECH        ", font = "arial 20 bold", bg='black',fg="white").pack()

Msg = StringVar()

Label(window,text ="Enter Your Text Here: ", font = 'arial 20 bold', fg ='darkgreen').place(x=5,y=60)

entry_field = Entry(window, textvariable = Msg ,width ='30',font = 'arial 15 bold',bg="lightgreen")

entry_field.place(x=5,y=100)

Button(window, text = "PLAY TEXT", font = 'arial 15 bold' , width = '20',bg = 'orchid',fg="white").place(x=35,y=140)

Button(window, font = 'arial 15 bold',text = 'RESET APPLICATION', width = '20',bg = 'darkgreen',fg="white").place(x=35 , y = 190)

Button(window, font = 'arial 15 bold',text = 'EXIT APPLICATION', width = '20' , bg = 'red',fg="white").place(x=35 , y = 240)

4. Creating a function for the button to convert text to speech

We would be defining three functions for the three buttons, The exit application button is pretty simple where we just have to destroy the window.

The next function i.e. the reset button which deletes the content of the entry box by setting it to a blank string. The last function is required to convert the text to voice which requires a few functions which are described below.

  1. get : To get the text entered in the entry box and store it in a variable
  2. gTTS : Convert the message passed to the function into a speech
  3. save : To save the speech in mp3 format
  4. playsound : To play the speech saved in the previous step
def Text_to_speech():
    Message = entry_field.get()
    speech = gTTS(text = Message)
    speech.save('data.mp3')
    playsound('data.mp3')

def Exit():
    window.destroy()

def Reset():
    Msg.set("")

The next step involves adding the command property to the button declaration connecting the functions to respective buttons.

Final Code to Convert Text to Speech

The complete final code of the project is shown below.

from tkinter import *
from gtts import gTTS
from playsound import playsound

def Text_to_speech():
    Message = entry_field.get()
    speech = gTTS(text = Message)
    speech.save('data.mp3')
    playsound('data.mp3')

def Exit():
    window.destroy()

def Reset():
    Msg.set("")
    
window = Tk()
window.geometry("350x300") 
window.configure(bg='#FAD7A0')
window.title("TEXT TO SPEECH")
 
Label(window, text = "        TEXT TO SPEECH        ", font = "arial 20 bold", bg='black',fg="white").pack()
Msg = StringVar()
Label(window,text ="Enter Your Text Here: ", font = 'arial 20 bold', fg ='darkgreen').place(x=5,y=60)

entry_field = Entry(window, textvariable = Msg ,width ='30',font = 'arial 15 bold',bg="lightgreen")
entry_field.place(x=5,y=100)

Button(window, text = "PLAY TEXT", font = 'arial 15 bold' , command = Text_to_speech ,width = '20',bg = 'orchid',fg="white").place(x=35,y=140)
Button(window, font = 'arial 15 bold',text = 'RESET APPLICATION', width = '20' , command = Reset,bg = 'darkgreen',fg="white").place(x=35 , y = 190)
Button(window, font = 'arial 15 bold',text = 'EXIT APPLICATION', width = '20' , command = Exit, bg = 'red',fg="white").place(x=35 , y = 240)

window.mainloop()

The Sample Output Video

The video below displays the working of the application. Check it out!

Conclusion

Congratulations! We have successfully built the text to speech python tkinter project. Hope you liked it!

Thank you for reading!