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
Installation
To installĀ gTTS API, open a command prompt and enter the following
pip install gTTS
Code
# 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 myobj.save("test.mp3") # Playing the converted file os.system("test.mp3")
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