The Google Text-to-Speech (gTTS) library in Python provides a simple way to convert text into speech. Whether you're building an application that needs to generate audio feedback, or you're just curious about adding a text-to-speech feature to your project, gTTS is an excellent tool to explore.
In this blog post, we’ll cover what gTTS is, how to install it, and demonstrate its capabilities with practical examples.
What is gTTS?
gTTS, short for Google Text-to-Speech, is a Python library and CLI tool that interfaces with Google's Text-to-Speech API. It converts written text into spoken words in a variety of languages, accents, and tones.
Some features of gTTS include:
- Support for multiple languages.
- Different speech accents.
- Ability to save audio files in MP3 format.
- Lightweight and easy to integrate into Python projects.
Installing gTTS
Before we can use gTTS, we need to install it. Open your terminal and run:
pip install gTTS
This will install the latest version of gTTS.
Basic Usage
Here’s a simple example of how to use gTTS to convert text to speech and save it as an audio file.
Example 1: Converting Text to Speech
from gtts import gTTS # Text to convert to speech text = "Hello, welcome to our tutorial on the gTTS library in Python!" # Create a gTTS object tts = gTTS(text, lang='en') # Save the speech to an audio file tts.save("welcome.mp3") print("Audio file saved as welcome.mp3")
Running this script will save the spoken version of the text in a file named welcome.mp3. You can then play it using any media player.
Customizing gTTS
1. Changing the Language
gTTS supports many languages. For instance, to use French, you can set lang='fr'.
from gtts import gTTS text = "Bonjour tout le monde! Ceci est un exemple de gTTS en français." tts = gTTS(text, lang='fr') tts.save("french_example.mp3") print("French audio file saved.")
2. Adding Accents
For some languages, gTTS allows specifying different accents. For example, you can choose between Indian, British, and American English:
text = "This is an example with a British English accent." tts = gTTS(text, lang='en', tld='co.uk') # tld specifies the accent tts.save("british_accent.mp3")
Here are some commonly used top-level domains (tld):
- com (default): American English
- co.uk: British English
- co.in: Indian English
Advanced Features
1. Reading Text from a File
If you have a large amount of text stored in a file, you can load and convert it to speech:
from gtts import gTTS # Open and read text from a file with open("example.txt", "r") as file: text = file.read() # Convert the text to speech tts = gTTS(text, lang='en') tts.save("file_audio.mp3") print("Audio file created from text file.")
2. Playing Audio Directly
You don’t need to save the audio file to play it. Using libraries like playsound, you can play the generated speech immediately:
from gtts import gTTS from playsound import playsound # Text to speech text = "This audio will play without saving to a file." tts = gTTS(text, lang='en') tts.save("temp.mp3") # Save temporarily # Play the audio playsound("temp.mp3")
Using gTTS in Real Applications
gTTS is versatile and can be used in a variety of projects:
- Voice assistants: Build a virtual assistant that responds with speech.
- Audiobooks: Convert written content into spoken audio.
- Language learning apps: Help users hear pronunciations in different languages.
- Accessibility tools: Provide text-to-speech support for visually impaired users.
Error Handling
When using gTTS, it’s essential to handle errors, especially when dealing with unsupported languages or network issues:
from gtts import gTTS import os try: text = "This is a test message." tts = gTTS(text, lang='en') tts.save("test.mp3") print("Audio file created successfully!") except Exception as e: print("An error occurred:", e)
Conclusion
The gTTS library is a powerful and easy-to-use tool for converting text to speech in Python. Whether you're working on a small project or building a full-fledged application, gTTS can bring your project to life with human-like speech.
Experiment with the examples above, and feel free to get creative by integrating gTTS into your next project.