max python

Just another WordPress site

Convert Text to Speech in Python using gTTS

In this article we show you how to turn text to speech in python using gTTS. What is that ?

gTTS (Google Text-to-Speech), a Python library and CLI tool to interface with Google Translate’s text-to-speech API. Write spoken mp3 data to a file, a file-like object (bytestring) for further audio manipulation, or stdout. Or simply pre-generate Google Translate TTS request URLs to feed to an external program


To installĀ  gTTS API, open a command prompt and enter the following

pip install gTTS



# Import the module 
from gtts import gTTS
import os
# The text that you want to convert to audio
mytext = 'Welcome to maxpython!'
# Language in which you want to convert
language = 'en'
# Pass the text and language to the engine, 
myobj = gTTS(text=mytext, lang=language, slow=False)
# Saving the converted audio in a mp3 file named"test.mp3")
# Playing the converted file

Now when you run this an mp3 file will be created and then played with your default player for mp3 files. On my system it was vlc


Leave a Comment

Your email address will not be published. Required fields are marked *

This div height required for enabling the sticky sidebar
Ad Clicks : Ad Views : Ad Clicks : Ad Views :