Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 64

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, instant speech. gTTS = online, natural voice, saves to a file.

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")
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

Pointpyttsx3gTTS
InternetNot neededRequired
Speaks instantlyYesSaves a file
Voice qualityRoboticMore natural
Change voice/speedYesLimited

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.
  • pyttsx3 works offline and speaks instantly.
  • Change voice and speed with setProperty in pyttsx3.
  • gTTS uses 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?
Use a text-to-speech library. 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?
Use 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?
With gTTS, call 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.
Do I need the internet for text to speech?
Only for gTTS, which uses Google's online service. pyttsx3 works completely offline, so choose pyttsx3 when you have no internet or want instant local speech.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicWeather API Program Your progress: 64 / 66 (97%) · Lesson 65 / 66 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free