Converting text to speech in Python

File handling | Python Methods and Functions

GTTS API supports multiple languages ​​including English, Hindi, Tamil, French, German and many more. Speech can be delivered at any of two available audio speeds, fast or slow. However, since the last update, it is not possible to change the voice of the generated sound.


To install the gTTS API, open a terminal and write

 pip install gTTS 

This works for any platform. 
We are now ready to write a sample program that converts text to speech.

# Import the required module for text
# to speech conversion

from gtts import gTTS

# This module is imported so we can
# play the converted audio

import os

# Text you want to convert to audio

mytext = 'Welcome to pythonengineering!'

# The language you want to convert in

language = 'en'

# Transfer of text and language to the engine,
# here we marked slow = False. Which says
# the module that needs the converted audio
# has a high speed

myobj = gTTS (text = mytext, lang = language, slow = False )

# Save converted audio to mp3 file with name
# welcome ( "welcome.mp3" )

# Play the converted file

os.system ( " mpg321 welcome.mp3 " )


 The output of the above program should be a voice saying, 'Welcome to pythonengineering!' 

This article contributed by Achil Goel . If you are as Python.Engineering and would like to contribute, you can also write an article using or by posting the article [email protected] ... See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.