gTTS (Google Text-to-Speech)

2.5.4 · active · verified Sat Apr 11

gTTS (Google Text-to-Speech) is an active Python library and command-line interface (CLI) tool that interacts with Google Translate's text-to-speech API. It converts text into spoken audio, which can be saved as an MP3 file or streamed to a file-like object. The library provides features for customizable text pre-processing and tokenization, enabling flexible and natural-sounding speech generation. As of its current version, 2.5.4, it continues to be actively maintained, with its latest release in November 2024.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `gTTS` with a string and a language, then save the generated speech as an MP3 file. The `slow` parameter can be set to `True` for slower speech.

from gtts import gTTS

# The text that you want to convert to audio
text_to_speak = "Hello world, this is a test of the gTTS library."

# Language in which you want to convert
language = 'en'

# Pass the text and language to the gTTS object
tts = gTTS(text=text_to_speak, lang=language, slow=False)

# Save the converted audio to a file
tts.save("hello_world.mp3")

print("Audio saved as hello_world.mp3")

view raw JSON →