How to Convert Text to Speech in Python
In Python, the conversion of text to speech typically uses libraries which interface with text-to-speech engines. The gTTS (Google Text-to-Speech) and pyttsx3 are among the most popular libraries for the task. The steps for the two methods in question are now discussed below:
1. With gTTS (Google Text-to-Speech)
gTTS is a simple library that sends text to Google’s TTS engine and converts it into speech. It supports multiple languages and accents.
Steps to use gTTS:
- Install gTTS: You can install the gTTS library using the following command:
pip install gTTS
2. Import the Library and Write Code: After installation, you can import the library and convert text to speech. Here’s a simple example:
from gtts import gTTS
import os
# The text you want to convert to speech
text = "Hello, welcome to Python programming!"
# Convert text to speech
tts = gTTS(text=text, lang='en', slow=False)
# Save the speech as an mp3 file
tts.save("output.mp3")
# Play the saved speech (optional)
os.system("start output.mp3") # For Windows
# os.system("mpg321 output.mp3") # For Linux
text
: Text you want to convert to speech.lang
: The speech language, i.e., en for English or es for Spanish.slow
: Whether to have the speech slow (True
orFalse
).
3. Running the Script: It creates an audio file called output.mp3 and plays it automatically through your system’s media player when the script is executed.
2. Using pyttsx3 (Offline Text-to-Speech)
Unlike gTTS, pyttsx3 is an offline library. It works offline and does not require any internet connection. It supports multiple speech engines: SAPI5 for Windows, NSSpeechSynthesizer for macOS, and espeak for Linux.
Steps to use pyttsx3:
- Install pyttsx3: You can install pyttsx3 using pip:
pip install pyttsx3
2. Import the Library and Write Code: Here’s an example to convert text to speech using pyttsx3:
import pyttsx3
# Initialize the pyttsx3 engine
engine = pyttsx3.init()
# Set properties like volume and rate (optional)
engine.setProperty('rate', 150) # Speed of speech (default is 200)
engine.setProperty('volume', 1) # Volume (0.0 to 1.0)
# The text you want to convert to speech
text = "Hello, this is an offline text-to-speech example."
# Convert the text to speech
engine.say(text)
# Wait for the speech to finish before exiting
engine.runAndWait()
rate
: Controls the speech pace (usually a default of about 200 words per minute).volume
: Adjusts the volume (0.0 to 1.0).
3. Running the Script: When you run the script, the text will be read out loud using your system’s default voice.
Comparing gTTS and pyttsx3
Feature | gTTS | pyttsx3 |
---|---|---|
Offline Support | No (requires an internet connection) | Yes (works offline) |
Speed | Depends on internet connection | Instant (no dependency on internet) |
Languages Supported | Multiple (via Google Translate API) | Limited (depends on the system) |
Voice Customization | Limited (but some control over language and speed) | Good (can change voice properties like rate, volume, pitch) |
Platform Compatibility | Cross-platform (requires internet) | Cross-platform (works offline) |
Conclusion
- gTTS is good when you want something easy and not bothered to look for internet support.
- pyttsx3 is better for offline usage and provides more control over voice parameters.
Both libraries have their advantages depending on the use case, so you can choose the one that fits your needs.