1 min read

Python gTTS Module

gTTS (Google Text-to-Speech) is a Python library and CLI tool to interface with Google Translate's text-to-speech API. It writes spoken text data to a file, a file-like object (bytestring) for further audio manipulation, or to stdout.

Installation

pip install gTTS

Basic Usage

from gtts import gTTS

tts = gTTS('Hello world')
tts.save('hello.mp3')

Language and Speed

You can specify the language (IETF language tag) and the speed of speech.

from gtts import gTTS

# English, slow speed
tts_en = gTTS('Hello', lang='en', slow=True)
tts_en.save('hello_slow.mp3')

# French, normal speed
tts_fr = gTTS('Bonjour', lang='fr', slow=False)
tts_fr.save('bonjour.mp3')

Working with File Objects

You can write to a file-like object instead of saving directly to disk.

from gtts import gTTS
from io import BytesIO

mp3_fp = BytesIO()
tts = gTTS('Hello', lang='en')
tts.write_to_fp(mp3_fp)

programming/python/python