Text to Speech in Python (pyttsx3 and gTTS)

For text to speech in Python, install pyttsx3, which works offline with your computer’s built-in voices, then call say() and runAndWait(). For natural-sounding MP3 files, gTTS uses Google’s online service:

import pyttsx3
engine = pyttsx3.init()
engine.say("Your order has shipped.")
engine.runAndWait()

I tested these on Windows 11 with Python 3.12.5, pyttsx3 2.99 and gTTS 2.5.4. Since you can’t hear a screenshot, I saved the speech to files and measured them instead of playing it aloud.

We’ll install both libraries, list and change voices, save speech to WAV and MP3, plot the audio, and compare the options.

Install a text to speech library

pip install pyttsx3     # offline, uses system voices
pip install gTTS        # online, Google Translate's voices

On Linux, pyttsx3 needs eSpeak (sudo apt install espeak-ng). Windows and macOS already include voices. More on installing in how to install a module in Python.

Text to speech in Python with pyttsx3: voices and settings

pyttsx3 talks to the speech engine your operating system already has. Start by checking which voices are installed and the default speed:

import pyttsx3

engine = pyttsx3.init()                  # uses SAPI5 on Windows, NSSpeechSynthesizer on macOS, eSpeak on Linux
for voice in engine.getProperty("voices"):
    print(voice.name, voice.languages)
print("rate:", engine.getProperty("rate"), "words per minute")
print("volume:", engine.getProperty("volume"))

Output:

Microsoft David Desktop - English (United States) ['en-US']
Microsoft Zira Desktop - English (United States) ['en-US']
rate: 200 words per minute
volume: 1.0
Command Prompt output of Python pyttsx3 listing the Microsoft David and Zira voices with the default rate and volume
Windows 11 ships with two US English voices, David and Zira; the default rate is 200 words per minute.

You can install more voices under Windows Settings > Time & language > Speech, and pyttsx3 picks them up automatically.

Save speech to a WAV file

save_to_file() writes the audio instead of playing it, which is what you want for apps, videos or phone menus. Here I slow it down and switch to Zira:

import wave
import pyttsx3

text = "Your order of $49.99 has shipped from our Austin warehouse and will arrive on Friday, November 27."

engine = pyttsx3.init()
engine.setProperty("rate", 170)          # a little slower than the default 200
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[1].id)  # Microsoft Zira on this machine
engine.save_to_file(text, "order_update.wav")
engine.runAndWait()

with wave.open("order_update.wav") as w:
    seconds = w.getnframes() / w.getframerate()
    print(f"saved order_update.wav: {seconds:.1f} s, {w.getframerate()} Hz, {w.getnchannels()} channel(s)")

Output:

saved order_update.wav: 8.6 s, 22050 Hz, 1 channel(s)
Command Prompt output of Python pyttsx3 saving an order update message to a WAV file and reporting its length
The sentence became an 8.6-second WAV file at 22,050 Hz, measured with Python’s built-in wave module.

Use engine.setProperty("volume", 0.8) for volume from 0.0 to 1.0. Remember to call runAndWait(); nothing is spoken or saved until you do.

Create an MP3 with gTTS (Google Text-to-Speech)

gTTS sends your text to Google Translate’s speech service and saves an MP3. It sounds more natural, but it needs an internet connection:

import os
from gtts import gTTS
from gtts.lang import tts_langs

text = "Welcome to the Denver store. Today's special is 20 percent off all winter jackets."
tts = gTTS(text, lang="en", tld="us")   # tld picks the accent; "us" = American English
tts.save("welcome.mp3")

print("saved welcome.mp3:", os.path.getsize("welcome.mp3"), "bytes")
print("languages available:", len(tts_langs()))
print("Spanish code:", [code for code, name in tts_langs().items() if name == "Spanish"])

Output:

saved welcome.mp3: 50496 bytes
languages available: 69
Spanish code: ['es']
Command Prompt output of Python gTTS saving a welcome message as an MP3 file and counting supported languages
gTTS saved the MP3 and supports dozens of languages; tld="us" keeps an American accent.

gTTS isn’t an official Google product, and heavy use can get rate-limited. For production apps, use a cloud speech API with a proper account.

Plot the speech audio

A WAV file is just numbers, so you can analyze it like any other data. Here’s the pyttsx3 file plotted with NumPy and matplotlib:

import wave
import matplotlib.pyplot as plt
import numpy as np

with wave.open("order_update.wav") as w:
    rate = w.getframerate()
    samples = np.frombuffer(w.readframes(w.getnframes()), dtype=np.int16)

t = np.arange(samples.size) / rate
plt.figure(figsize=(9, 3.8))
plt.plot(t, samples, lw=0.4)
plt.title("Waveform of order_update.wav (pyttsx3, Microsoft Zira)")
plt.xlabel("Seconds"); plt.ylabel("Amplitude")
plt.tight_layout()
plt.show()
Matplotlib waveform of a WAV file created with Python pyttsx3 text to speech showing words as bursts of amplitude
Each burst is a word or phrase; the flat gaps are pauses.

Signal processing on audio like this is covered in FFT in Python.

pyttsx3 vs gTTS: which should you use?

pyttsx3gTTS
Internet neededNoYes
Voice qualityRobotic to decent (system voices)Natural
OutputSpeak aloud or WAVMP3 file
Change speed and voiceYesOnly slow mode and accent
LanguagesWhatever voices are installedDozens

More Python project ideas:

Frequently asked questions

How do I do text to speech in Python?

Install pyttsx3, then run engine = pyttsx3.init(), engine.say("text") and engine.runAndWait().

Does pyttsx3 work offline?

Yes. It uses the voices built into Windows, macOS or eSpeak on Linux.

How do I save text to speech as an MP3?

Use gTTS: gTTS("text", lang="en").save("file.mp3"). pyttsx3 saves WAV with save_to_file().

How do I change the voice or speed?

In pyttsx3, set engine.setProperty("voice", voice_id) and engine.setProperty("rate", 170).

Why does pyttsx3 say nothing?

You probably didn’t call runAndWait(), or no speech engine is installed (install eSpeak on Linux).

Where is pyttsx3 documented?