Python GTTS Library: How to Convert Text to Speech Easily

The Python GTTS (Google Text-to-Speech) library is a powerful tool for converting text into spoken words using Google's Text-to-Speech API. With the GTTS library, you can easily generate an MP3 file from any text input, which can then be played on your local system. This is particularly useful for creating audio versions of articles, books, or other written content.

One of the key advantages of the GTTS library is its flexibility. You can specify the language of the speech output, choose from a variety of different voices, and adjust the speed of the speech. This allows you to create audio files that are tailored to your specific needs and preferences.

In addition to these features, the GTTS library also provides a number of options for saving and manipulating the output. For example, you can save the speech output as an MP3 file, which can then be used in a variety of different applications. You can also specify the location and filename of the output file, making it easy to organize and manage your audio files.

Overall, the GTTS library is a powerful and versatile tool for anyone who needs to convert text into spoken words. Whether you are looking to create audio versions of your written content, or you simply need a way to generate speech output for your Python programs, the GTTS library is a great choice.

How to Install and Use gtts Library in Python: A Step-by-Step Guide for Text-to-Speech Conversion

You can install the GTTS (Google Text-to-Speech) library in Python using the pip package manager. Here are the steps:

  1. Open a command prompt or terminal on your computer.
  2. Type the following command and press Enter to install the GTTS library:
  3. pip install gtts                          
    
    

Here is an example code snippet that demonstrates how to use the GTTS library to generate speech from text:

from gtts import gTTS
import os
  
text = "Hello, how are you today?"
language = 'en'
  
tts = gTTS(text=text, lang=language)
tts.save("hello.mp3")
  
os.system("mpg321 hello.mp3")                          

In this example, we import the gTTS class from the gtts module and create an instance of it with the text and language specified. We then save the speech output as an MP3 file using the save() method, and use the os module to play the MP3 file using the mpg321 command line tool.

Overall, the Python GTTS library is a simple and easy-to-use library for converting text into speech using Google's Text-to-Speech API. It can be useful for various applications, such as creating automated voice notifications or generating speech output for visually impaired users.

Python GTTS Library Tutorial: Transform Text into Audio in Minutes

This code uses the gTTS library, to convert the text from a file named sample.txt into an audio file in MP3 format. The resulting audio file is saved as audio.mp3.

Here's a breakdown of the code:

This line imports the gTTS class from the gtts module.

from gtts import gTTS

These two lines open the sample.txt file, read its contents, and store them in the data variable.

file = open('sample.txt')
data = file.read()

This line sets the language of the text to be English.

language = 'en'

This line creates an instance of the gTTS class, passing the data variable as the text to be converted to speech.

audio = gTTS(text=data)

This line saves the resulting audio as an MP3 file named audio.mp3.

audio.save('audio.mp3')

Overall, this code can be used to add text-to-speech functionality to a webpage, allowing users to listen to the content instead of having to read it.

You can watch the video below:

How to Build a Text-to-Speech Converter GUI App using Python Tkinter, GTTS and Playsound Libraries

This code is a basic GUI (Graphical User Interface) program using the tkinter, gtts and playsound libraries to implement a simple Text-To-Speech application.

Here is the step-by-step explanation of the code:

from tkinter import * - It imports all the classes, functions, and variables from the tkinter library.

from gtts import gTTS - It imports the gTTS class from the gtts library, which is used to convert text to speech.

from playsound import playsound - It imports the playsound function from the playsound library, which is used to play the generated audio.

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

root = Tk() - It creates an instance of the Tk class, which represents the main window of the application.

root.geometry('350x300') - It sets the size of the main window to 350x300 pixels.

root.resizable(0,0) - It disables the resizing of the main window.

root.config(bg='white') - It sets the background color of the main window to white.

root.title('TEXT-TO-SPEECH') - It sets the title of the main window to 'TEXT-TO-SPEECH'.

root = Tk()
root.geometry('350x300')
root.resizable(0,0)
root.config(bg='white')
root.title('TEXT-TO-SPEECH')

Label(root, text='Text-TO-SPEECH' , font='ariel 20 bold', bg='white').pack() - It creates a label widget with the text 'Text-TO-SPEECH', sets its font and background color, and packs it into the main window.

Label(root, text='SAMCODEHUB', font='ariel 15 bold', bg='white').pack(side= BOTTOM) - It creates a label widget with the text 'SAMCODEHUB', sets its font and background color, and packs it into the main window at the bottom.

# heading
Label(root, text='Text-TO-SPEECH' , font='ariel 20 bold', bg='white').pack()
Label(root, text='SAMCODEHUB', font='ariel 15 bold', bg='white').pack(side= BOTTOM)

Label(root, text='Enter Text', font='ariel 15 bold', bg='white').place(x=20, y=60) - It creates a label widget with the text 'Enter Text', sets its font and background color, and places it at the specified position.

# label 
Label(root, text='Enter Text', font='ariel 15 bold', bg='white').place(x=20, y=60)

Msg= StringVar() - It creates a StringVar variable, which is used to store the user input from the text entry field.

# text variable
Msg= StringVar()

entry_field = Entry(root, textvariable =Msg, width='50') - It creates a text entry widget with the specified width and sets its text variable to Msg.

entry_field.place(x=20, y=100) - It places the text entry widget at the specified position.

#ENTRY
entry_field = Entry(root, textvariable =Msg, width='50')
entry_field.place(x=20, y=100)

def Text_to_speech(): - It defines a function Text_to_speech, which is called when the 'PLAY' button is clicked.

Message = entry_field.get() - It gets the user input from the text entry widget.

speech = gTTS(text= Message) - It creates a gTTS object with the user input text.

speech.save('v.mp3') - It saves the generated audio as a .mp3 file with the name 'v.mp3'.

playsound('v.mp3') - It plays the generated audio using the playsound function.

# define function
def Text_to_speech():
    Message = entry_field.get()
    speech = gTTS(text= Message)
    speech.save('v.mp3')
    playsound('v.mp3')

def Exit(): - It defines a function Exit, which is called when the 'EXIT' button is clicked.

root.destroy() - It destroys the main window, which exits the program.

def Exit():
    root.destroy()

def Reset(): - This function resets the content of the Msg variable to an empty string.

def Reset():
    Msg.set()

This lines creates 3 Buttons widgets with the label "RESET" and sets its command to the Reset() function. When the button is clicked, it will execute the Reset() function.

Button widget with the label "PLAY" and sets its command to the Text-To-Speech() function. When the button is clicked, it will execute the Text-To-Speech() function.

Button widget with the label "EXIT" and sets its command to the Exit() function. When the button is clicked, it will execute the Exit() function.

# button
Button(root, text = "PLAY", font= 'ariel 15 bold', command = Text_to_speech, width=4).place(x=25, y=140)   
Button(root, text= "EXIT", font= 'ariel 15 bold', command = Exit, bg='red').place(x=100, y=140)
Button(root, text= "RESET", font= 'ariel 15 bold', command = Reset).place(x=175, y=140) 

The mainloop() method is responsible for starting the event loop, which waits for user events and processes them as they occur.

root.mainloop()

In this tutorial, we have learned how to create a text-to-speech converter GUI app using Python's tkinter and gtts libraries. We have covered the basics of these libraries and provided a step-by-step guide for building the app from scratch. By following this tutorial, you can create a customizable text-to-speech converter that can read out any text you input. With some additional modifications, you can also enhance the app's functionality to suit your needs. We hope you found this tutorial useful and that it has inspired you to explore more possibilities with Python!

You can watch the video below:

Comments...

banner