Home Python Project Python text to Speech

Python text to Speech

by anupmaurya
62 minutes read
Python text to Speech

In this article, you’ll learn how we can create a program which convert text to Speech using Python.

Text to speech is a process to convert any text into voice. Text to speech project takes words on digital devices and convert them into audio with a button click or finger touch. Text to speech python project is very helpful for people who are struggling with reading.

To implement this project, we will use the basic concepts of Python, Tkinter, gTTS, and playsound libraries.

  • Tkinter is a standard GUI Python library that is one of the fastest and easiest ways to build GUI applications using Tkinter.
  • gTTS (Google Text-to-Speech) is a Python library, which is a very easy library that converts the text into audio.
  • The playsound module is used to play audio files. With this module, we can play a sound file with a single line of code.

To install the required libraries, you can use pip install command:

pip install tkinter
pip install gTTS
pip install playsound

In this project, we add a message which we want to convert into voice and click on play button to play the voice of that text message.

  • Importing the modules
  • Create the display window
  • Define functions

So these are the basic steps that we will do in this Python project. Let’s start.

1. Import Libraries

Let’s start by importing the libraries: tkinter, gTTS, and playsound

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

 Initializing window

root = Tk()
root.config(bg = '#FFE873')
  • Tk() to initialized tkinter which will be used for GUI
  • geometry() used to set the width and height of the window
  • configure() used to access window attributes
  • bg will used to set the color of the background
  • title() set the title of the window
Label(root, text = 'TEXT TO SPEECH' , font='Verdana 30 bold' , bg ='white smoke').place(x=30,y=10)
Label(root, text ='Techarge' , font ='Verdana 15 bold', bg = 'white smoke').place(x=190,y=260)
Label(root, text ='Enter Text', font ='Verdana 15 bold', bg ='white smoke').place(x=30,y=70)

Msg = StringVar()

entry_field = Entry(root,textvariable =Msg, width ='50')
entry_field.place(x=30 , y=120)

Label() widget is used to display one or more than one line of text that users can’t able to modify.

  • root is the name which we refer to our window
  • text which we display on the label
  • font in which the text is written
  • pack organized widget in block
  • Msg is a string type variable
  • Entry() used to create an input text field
  • textvariable used to retrieve the current text to entry widget
  • place() organizes widgets by placing them in a specific position in the parent widget

3. Function to Convert Text to Speech in Python

def Text_to_speech():
    Message = entry_field.get()
    speech = gTTS(text = Message)
  • Message variable will stores the value of entry_field
  • text is the sentences or text to be read.
  • lang takes the language to read the text. The default language is English.
  • slow use to reads text more slowly. The default is False.

As we want the default value of lang, so no need to give that to gTTS.

  • speech stores the converted voice from the text
  • speech.save(‘DataFlair.mp3’) will saves the converted file as DataFlair as mp3 file
  • playsound() used to play the sound

4. Function to Exit

def Exit():

root.destroy() will quit the program by stopping the mainloop().

5. Function to Reset

def Exit():

Reset function set Msg variable to empty strings.

6. Define Buttons

Button(root, text = "PLAY" , font = 'Verdana 15 bold', command = Text_to_speech, width =4).place(x=30, y=150)
Button(root,text = 'EXIT',font = 'Verdana 15 bold' , command = Exit, bg = 'OrangeRed1').place(x=100,y=150)
Button(root, text = 'RESET', font='Verdana 15 bold', command = Reset).place(x=185 , y =150)

Button() widget used to display button on the windowroot.mainloop()


root.mainloop() is a method that executes when we want to run our program.

Source Code with comments


So, here is our Text to Speech . Simple isn’t it?? This is how we have successfully done with the ‘text to Speech using Python’. I hope the ‘Tkinter , Playsound, and gTTS library is now more clear to you and don’t forget to try this code once!!

You can play around with the library, explore more features and even customize it further and Don’t forget to share with your friends.

ThankYou Pythoner’s!!! 🙂

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.