Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Convert Text to Speech in Python?
Converting Text to Speech (TTS) allows you to transform written text into spoken audio. Python offers the text to speech conversion with the help of APIs. One such API which serves this purpose is the Google Text to Speech API, known as gTTS.
The gTTS enables conversion of the provided text into speech and saves the output as an MP3 audio file.
Installing gTTS
To use the gTTS Text to Speech conversion tool, we need to install it first using pip ?
pip install gTTS
Basic Text to Speech Conversion
Here's a simple example that converts text to speech and saves it as an MP3 file ?
from gtts import gTTS
# Text to be converted to speech
my_text = "I want to learn Python"
# Create gTTS object
speech = gTTS(text=my_text, lang="en", slow=False)
# Save the audio file
speech.save("tts.mp3")
print("Audio file saved as tts.mp3")
Audio file saved as tts.mp3
gTTS Parameters
The gTTS() function accepts three main parameters ?
- text: The input text you want to convert into speech
- lang: The language code (e.g., "en" for English, "es" for Spanish, "fr" for French)
- slow: Boolean value that controls audio speed (False for normal, True for slower speech)
Playing the Generated Audio
To play the generated audio file directly, you can use the os.system() method ?
from gtts import gTTS
import os
# Create and save the audio
my_text = "Hello, this is a text to speech example"
speech = gTTS(text=my_text, lang="en", slow=False)
speech.save("example.mp3")
# Play the audio (Windows)
os.system("start example.mp3")
# For macOS, use: os.system("afplay example.mp3")
# For Linux, use: os.system("mpg123 example.mp3")
Different Languages and Speed Control
gTTS supports multiple languages and speech speeds ?
from gtts import gTTS
# English - Normal speed
text_en = "Hello, welcome to Python programming"
tts_en = gTTS(text=text_en, lang="en", slow=False)
tts_en.save("english_normal.mp3")
# English - Slow speed
tts_slow = gTTS(text=text_en, lang="en", slow=True)
tts_slow.save("english_slow.mp3")
# Spanish
text_es = "Hola, bienvenido a la programación Python"
tts_es = gTTS(text=text_es, lang="es", slow=False)
tts_es.save("spanish.mp3")
print("Multiple audio files created successfully")
Multiple audio files created successfully
Supported Languages
Some commonly used language codes include ?
| Language | Code | Example |
|---|---|---|
| English | en | Hello World |
| Spanish | es | Hola Mundo |
| French | fr | Bonjour le monde |
| German | de | Hallo Welt |
| Hindi | hi | ?????? ?????? |
Complete Example with Error Handling
Here's a more robust example with error handling ?
from gtts import gTTS
import os
def text_to_speech(text, language="en", slow=False, filename="output.mp3"):
try:
# Create gTTS object
tts = gTTS(text=text, lang=language, slow=slow)
# Save the audio file
tts.save(filename)
print(f"Audio saved as {filename}")
return True
except Exception as e:
print(f"Error: {e}")
return False
# Example usage
sample_text = "Python makes text to speech conversion easy and efficient"
success = text_to_speech(sample_text, language="en", slow=False, filename="sample.mp3")
if success:
print("Text to speech conversion completed successfully!")
Audio saved as sample.mp3 Text to speech conversion completed successfully!
Conclusion
The gTTS library provides a simple way to convert text to speech in Python. It supports multiple languages and speed controls, making it versatile for various applications. Remember that gTTS requires an internet connection as it uses Google's online service.
