apyhub
Cover illustration for Text to Speech in Python: 5 Ways (Offline, Free and API)
ApyHub

Text to Speech in Python: 5 Ways (Offline, Free and API)

Text to Speech in Python: 5 Ways (Offline, Free and API)

01Introduction

To convert text to speech in Python, install a library such as pyttsx3 (offline) or gTTS (saves an MP3), pass it your text, and play or save the audio. For natural-sounding offline voices, use Piper. For web apps, servers and documents, a text to speech API returns an MP3 from one HTTP request, with nothing to install.

MethodInternet neededOutputVoice qualityBest for
pyttsx3NoWAV, or plays aloudRobotic (your OS voices)Quick offline scripts
PiperOnly to download a voiceWAVNatural (neural)Offline apps that should sound good
gTTSYesMP3Clear, one voice per languagePrototypes
edge-ttsYesMP3Natural (neural)Personal projects
Text to speech APIYesMP3Natural, same on every machineWeb apps, servers, documents

021. pyttsx3: offline and built in

pyttsx3 uses the speech engine already on your computer (SAPI5 on Windows, NSSpeechSynthesizer on macOS, eSpeak on Linux), so nothing leaves your machine.

· python
import pyttsx3  # pip install "pyttsx3==2.98"

engine = pyttsx3.init()
engine.save_to_file("Your order has shipped.", "order.wav")
engine.runAndWait()

Good: free, offline and private. Watch out: the voice sounds robotic and changes from one operating system to another. In our test on Linux, the latest version (2.99) ran save_to_file() without an error but never wrote the file, while 2.98 worked. On Linux servers, install eSpeak first (sudo apt install espeak-ng).

032. Piper: natural voices, still offline

Piper runs small neural voice models on your own CPU. After a one-time voice download (python -m piper.download_voices en_US-lessac-medium), everything stays offline.

· python
import wave
from piper import PiperVoice  # pip install piper-tts

voice = PiperVoice.load("en_US-lessac-medium.onnx")
with wave.open("order.wav", "wb") as f:
    voice.synthesize_wav("Your order has shipped.", f)

Good: natural voices, many languages, fast enough for a laptop or a Raspberry Pi. Watch out: you download and manage the voice models yourself, and the output is WAV.

043. gTTS: free MP3s from Google Translate's voice

· python
from gtts import gTTS  # pip install gTTS

gTTS("Your order has shipped.", lang="en").save("order.mp3")

Good: one line, MP3 output, many languages. Watch out: it uses an unofficial Google Translate endpoint, not the paid Google Cloud product, so there's no SLA and heavy use gets rate limited.

054. edge-tts: neural voices from Microsoft Edge

edge-tts uses the online service behind Edge's Read Aloud feature. It's the most natural of the free online options, and edge-tts --list-voices shows every voice available.

· bash
pip install edge-tts
edge-tts --voice en-US-AriaNeural --text "Your order has shipped." --write-media order.mp3

Watch out: like gTTS, it depends on an unofficial service that can change or stop working without notice. Fine for personal projects, risky for a product.

065. A text to speech API: one request, an MP3 back

When speech is part of a product (order notifications, narrated articles, accessibility features, voice replies from an AI agent), you want a supported service that sounds the same on every server. ApyHub's Text to Speech API takes text and returns a link to an MP3:

· python
import os, requests

r = requests.post(
    "https://api.eu.apyhub.com/apyhub/convert-text-to-speech/text/link",
    headers={"apy-token": os.environ["APY_TOKEN"]},
    json={"text": "Your order has shipped.", "gender": "female"},
)
mp3_url = r.json()["data"]  # signed link, valid for 15 minutes

What you get:

  • Up to 75,000 characters per request (roughly 12,000 words), so a long article is a single call.
  • A male or female voice, set with gender.
  • Documents as input. Send a PDF or Word file URL instead of text and get an MP3 of its contents. See PDF to speech: turning documents into audio.

What it costs: 750 atoms per request, whatever the length. The free plan covers 4 requests a month to try it. On paid plans a request works out to about $0.06–$0.12, so narrating a 1,500-word article costs a few cents. See pricing.

Watch out: there are two voices. If you need dozens of voices, voice cloning or fine pronunciation control, Google Cloud Text-to-Speech, Amazon Polly, Azure Speech, OpenAI or ElevenLabs offer more, with more setup and their own pricing.

Try the Text to Speech API free →

07Which option should you use?

  1. Offline, and the voice doesn't matter? pyttsx3.
  2. Offline, and it should sound natural? Piper.
  3. A quick script that needs an MP3? gTTS.
  4. A personal project with natural voices? edge-tts.
  5. Part of a product, on servers, or reading documents? A text to speech API.

08Common errors

  • pyttsx3 creates no file: pin version 2.98 and call runAndWait() after save_to_file().
  • pyttsx3 OSError on Linux: eSpeak is missing (sudo apt install espeak-ng).
  • An empty 0-byte MP3 from gTTS or edge-tts: both create the file before connecting, so a network failure leaves it empty. Check your connection or firewall.
  • gTTS 429 Too Many Requests: you hit the unofficial rate limit. Cache audio you've already generated, or move to an API.
  • API returns 400: the body needs both text and gender (male or female). See our guide to 400 Bad Request errors.

09Let an AI agent speak

The Text to Speech API is also available through ApyHub MCP, so Claude, ChatGPT, Cursor or Mistral Le Chat can call it directly: "turn this summary into an MP3." Every API in the ApyHub catalog works the same way, so an agent can summarize a document and narrate it in one conversation. To try the request before writing code, save it in Voiden, a free, open-source API client.

10Conclusion

pyttsx3 is the quickest offline option and Piper the offline option that sounds natural. gTTS and edge-tts give you free MP3s from online voices, and a text to speech API gives you supported, consistent audio for products, servers and documents. Experiment with a library, and switch to an API once your users rely on the audio.

Start free on ApyHub, no card required →

11FAQ

How do I convert text to speech in Python?

Install gTTS and run gTTS("Hello", lang="en").save("hello.mp3"), or use pyttsx3 to work offline.

What is the best text to speech library for Python?

pyttsx3 for quick offline use, Piper for natural offline voices, gTTS for simple MP3s, edge-tts for natural free online voices. For production, use a text to speech API.

What is the best offline text to speech for Python?

Piper. It runs neural voices locally and sounds far more natural than pyttsx3.

How do I save text to speech as an MP3 in Python?

gTTS, edge-tts and text to speech APIs produce MP3 directly. pyttsx3 and Piper save WAV, which ffmpeg can convert.

Is gTTS free?

The library is free, but it relies on an unofficial Google Translate endpoint with no guarantees and possible rate limits.

Why does pyttsx3 not save a file?

On Linux, pyttsx3 2.99 can finish save_to_file() without writing anything. Install 2.98 and call runAndWait() afterwards.

How much does a text to speech API cost?

It varies by provider. ApyHub charges 750 atoms per request of up to 75,000 characters, about $0.06–$0.12 on paid plans, with 4 free requests a month.

12About ApyHub

ApyHub is a curated API catalog and the trusted operational layer between your application and the services it depends on. One subscription, billed in atoms, covers the whole catalog of over 1,500 endpoints and capabilities, and it keeps growing. Every service is evaluated before it is listed and carries machine-readable certification covering GDPR, SOC 2 and ISO 27001. Every endpoint is MCP-ready by default. ApyHub runs dedicated EU and US regions and serves more than 65,000 developer workspaces every month. Start on the free tier with no card, or publish your own API on ApyHub.

13Sources