Text to Speech in Python
In this lesson: Convert text to speech in Python with pyttsx3 (offline) and gTTS — voices, speed and save MP3 — full code. In Hindi & English.
What we are building
A text-to-speech program turns written text into spoken audio — so your program can actually talk. Python has two popular libraries: pyttsx3 (works offline, speaks through your speakers) and gTTS (uses Google online, saves an audio file). Both are easy to use.
pyttsx3 — offline speech
Install it once, then make your program talk:
pip install pyttsx3
import pyttsx3
engine = pyttsx3.init()
engine.say("Hello, welcome to CodingEasily")
engine.runAndWait()
engine.say() queues the text, and engine.runAndWait() actually speaks it. No internet is needed — it plays through your speakers straight away.
Voice and speed
engine = pyttsx3.init()
engine.setProperty("rate", 150) # speaking speed
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[1].id) # switch voice
engine.say("This is a slower, different voice")
engine.runAndWait()
rate controls speed (lower is slower). getProperty("voices") lists available voices, and you pick one by its id. This lets you customise how the speech sounds.
gTTS — online, save to file
gTTS uses Google's service for a more natural voice and saves an MP3.
pip install gTTS
from gtts import gTTS
tts = gTTS("Hello from CodingEasily")
tts.save("output.mp3")
print("Saved output.mp3")
gTTS(text) creates the speech and save() writes it to an MP3 file you can play or share. It needs the internet because it calls Google's online service.
Which one to use
| Point | pyttsx3 | gTTS |
|---|---|---|
| Internet | Not needed | Required |
| Speaks instantly | Yes | Saves a file |
| Voice quality | Robotic | More natural |
| Change voice/speed | Yes | Limited |
Use pyttsx3 for offline, instant speech; use gTTS when you want a natural voice or an audio file to keep.
Common mistakes
- Forgetting
runAndWait(). In pyttsx3, the program may end before it finishes speaking without it. - Expecting gTTS to speak directly. gTTS saves a file — you then play it with a media player.
- No internet for gTTS. It fails offline; use pyttsx3 instead.
- Wrong voice index. The number of voices varies by system — check the list first.
Summary
- Text-to-speech turns text into spoken audio.
pyttsx3works offline and speaks instantly.- Change voice and speed with
setPropertyin pyttsx3. gTTSuses Google online and saves an MP3 file.- Pick pyttsx3 for offline speech, gTTS for a natural voice or a file.
Next, build another fun project like a chatbot, or fetch live data with a weather API.
Frequently Asked Questions
How do I make Python speak text aloud?
pyttsx3 works offline and speaks through your speakers directly. gTTS uses Google online and saves an MP3 file. Both turn a string of text into spoken audio.What is the difference between pyttsx3 and gTTS?
pyttsx3 works offline (no internet), speaks instantly, and lets you change voice and speed. gTTS needs the internet, sounds more natural, and saves to an audio file you can play or share.How do I change the voice or speed in pyttsx3?
engine.setProperty("rate", 150) to change speed and engine.setProperty("voice", voices[1].id) to switch voices. You get the available voices with engine.getProperty("voices").How do I save the speech as an audio file?
tts.save("output.mp3"). With pyttsx3, use engine.save_to_file(text, "out.mp3") then engine.runAndWait(). This creates an audio file instead of just speaking.