Convert Text to Speech in Python - GeeksforGeeks

15 Aug, 2021
There are several APIs available to convert text to speech in Python. One of such APIs is the Google Text to Speech API commonly known as the gTTS API. gTTS is a very easy to use tool which converts the text entered, into audio which can be saved as a mp3 file.
The gTTS API supports several languages including English, Hindi, Tamil, French, German and many more. The speech can be delivered in any one of the two available audio speeds, fast or slow. However, as of the latest update, it is not possible to change the voice of the generated audio.
YouTubeGeeksforGeeks501K subscribersConvert Text to Speech Using Python | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 6:56•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=buoSzgX9yM4" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
To install the gTTS API, open terminal and write
pip install gTTS

This works for any platform.Now we are all set to write a sample program that converts text to speech.
# Import the required module for text # to speech conversionfrom gtts import gTTS  # This module is imported so that we can # play the converted audioimport os  # The text that you want to convert to audiomytext = 'Welcome to geeksforgeeks!'  # Language in which you want to convertlanguage = 'en'  # Passing the text and language to the engine, # here we have marked slow=False. Which tells # the module that the converted audio should # have a high speedmyobj = gTTS(text=mytext, lang=language, slow=False)  # Saving the converted audio in a mp3 file named# welcome myobj.save("welcome.mp3")  # Playing the converted fileos.system("mpg321 welcome.mp3")
The output of the above program should be a 
voice saying, 'Welcome to geeksforgeeks!'

This article is contributed by Akhil Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
python-utility
Machine Learning
Python
Machine Learning
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
ML | Linear Regression
Decision Tree
Search Algorithms in AI
Python | Decision tree implementation
Reinforcement learning
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe