Text to Speech

Text to Speech

## Convert Text to Audio

Hello Techies, Welcome. In this article, we will be discussing about converting a text input to audio output using python and tkinter. So, let's get started.

Prerequisites:

  • python

  • tkinter

  • gtts

  • os

Let's understand what are these for..

Tkinter: Tkinter is the python module which supports the creation of GUI applications. Confused? Let me clarify.

What is a GUI Application?:

GUI stands for Graphical User Interface. Python has the amazing module tkinter for creating the user interface application. A user interface is basically the UI or frontend of the application with which the user interacts. It contains the Buttons, fields, Checkboxes etc. We will be creating one such very simple application in this project.

Run the following command to install tkinter.

pip install tkinter

You can find more details about tkinter module here

gtts: gtts stands for google translate text to speech . This module is used for converting the text to speech. In this project we will import the gTTS library to use the google text to speech API. Run the following command to install gtts.

pip install gtts

os: os module is used to perform the operations on the files of your local computer. Following command installs os module.

pip install os

Yayy!! Ready to go. Now, let's start coding.

The code is available at my Github repo

Step 1: Importing modules

'''importing modules'''
from gtts import gTTS
from tkinter import *
from tkinter import messagebox
import tkinter as tk
import os

The above code will import all the required modules.

Step 2: Creating Frame

Frames are the main windows which the user sees when the application is opened.

master = tk.Tk(className='Text to Speech Converter') #main window
master.geometry("500x500")
mainFrame =  Frame(master)
mainFrame.pack()

The tk.Tk() takes a className to create the main windows to the application and stores it in the master variable. Then we will use the geometry function to define the dimensions of the main window. Next Frame() uses the master variable to make a Frame. The pack() method arranges the widgets in blocks and places them in the main frame mainFrame

Step 3: Creating a text box for user input

#text box creation with label
myText_str = tk.StringVar() #string variable to get the contents of entry box
Label(mainFrame, text='Enter your text').grid(row=0) #label for text box
myText = Entry(mainFrame,textvariable=myText_str,bd=1,bg='white')
myText.grid(row=0,column=10)

We will use tk.StringVar() to get the contents of the text entered in textbox as a string and store it in myText_str variable. The Label() function takes the arguments like frame object mainFrame and text to give a label name to textbox and arranges it in grid using .grid(). Next we need an input text box which will be created using the Entry() method that takes the previously created string myText_str in textvariable argument,bd for dimensions and bg for background color. Then the entry box will be arranged in a grid with myText.grid()

Step 4: Text to Speech conversion

#text to speech conversion function
def text_to_speech():
    language = 'en'
    TextEntered = myText_str.get()
    if len(TextEntered)==0:
        messagebox.showerror("Error","you need to enter some text to get audio output")
    else:
        messagebox.showinfo("message",f"your input is {TextEntered}")
        obj = gTTS(text=TextEntered,lang=language,slow=False)
        obj.save("audio.mp3")
        os.system("audio.mp3")

In this step, we will create a function text_to_speech which converts the text into speech and then saves it in the system. First, we specify a language in which the audio needs to be pronounced. Then we need to get the entered text from the input box, which can be done by myText_str.get(). In the next line, we use if condition to check whether the user entered any input or not. This is because an empty input cannot produce any audio output. If the user entered an empty string then we will show an error messagebox messagebox.showerror("Error","you need to enter some text to get audio output"). Otherwise, we will say the user about the entered text. Now, the magic happens. The gTTS() method takes the TextEntered variable which contains the string input text from the user, and language in the lang argument. That's it. Now we will save the converted object using obj.save('filename.mp3'). to save it in the system use os.system('filename.mp3'). The audio file will be saved in your project location.

Hmm, missed something??

On what action does the text will be converted to audio? Okay, now scroll down.

#adding Button
buttonFrame = Frame(master)
buttonFrame.pack()
convert_button = tk.Button(buttonFrame, text='Convert',fg='black',activebackground='white',activeforeground='pink',width=20,bg='white',
                           borderwidth=0,command=text_to_speech)
convert_button.grid(row=20,column=1)
convert_button.pack()


master.mainloop()

So, we will be creating a button. when the user clicks on it after entering the input he will get the audio output. We will do the samething as for textbox that is creating the frame and packing it. Then we will create the actual button convert_button which takes the following arguments. text- text to be appeared on button fg- color of the text activebackground- button background color activeforeground-button foreground color width-button width bg- button color borderwidth-button border command-action to perform (here we will pass the text_to_speech function) Don't forget to grid and pack the button. So the final step--> master.mainloop() it runs the event loop. This is the important line to be included in the tkinter app.

Step 5: Now, it's time to run the code.

python textToSpeech.py

which opens the following window.

gtts.PNG

messagebox after entering the text.

msgbox.PNG

Error message on empty text.

errormsg.PNG

Thanks for Reading. Happy Coding :)

You can contact me on Twitter Linkedin

Did you find this article valuable?

Support Lakshmi Sowjanya's Blog by becoming a sponsor. Any amount is appreciated!