Python has thousands of Libraries and APIs which can be used to build sophisticated programs using pre-written code. Here in this article, we will discuss how can we write a python program that can convert text to speech. However, we can build the program from scratch using python core code, but that would be a heck of a task and take too much time, so here we will use a python API ‘gTTS’ which will serve our purpose within 10 to 12 lines of code.
Prerequisite to Build the Python text to speech program
- Python latest version(3.8)
- gSST latest version (2.1.1)
Although you can use any text editor to write the python script, but you require a python interpreter on your system to execute the script. So, for this program, we suggest you download the latest version of python.
To install python, you can visit its official website, and download the appropriate edition for your system, for more detail on how to install python click here.
pip is the python package management tool that can be used to install and uninstall the various packages for python that are registered in PyPI. pip is a command-line tool, which helps you to install and manage all python packages using a command terminal. If you have installed the latest version of python, then you do not need to install pip separately because it comes along with python. However, if you are using the older versions of python, then you might need to update or install pip separately. Instead of installing pip separately, we suggest you install the latest version of python. For more information about how to install pip, click here.
gTTS stand for Google-text-to-speech, it is an API designed initially by google for android devices, but now it is also available for python. In python, gTTS defined as a python library and CLI tool with google translation API. Google translation API because it uses the default google voice to speak the text. Using gTTS we can convert any text to a .mp3 file and then using the program we play that file.
gTTS support multiple languages such as Banga, Hindi, English, Hungarian, Khmer, Koran, Tamil, Thai, etc.
Before using gTTS in our python script, we need to install it. To install any python package, we use the pip command.
pip install gTTS
this command will install the latest version of gTTS package for your python.
Python Program to Convert text to speech
#import gTTS class from gtts library to convert the text to speech from gtts import gTTS #import os module to paly the speech stored as .mp3 import os # the text you want to speech text = "Hello, Tech lovers Welcome to TechGeekBuzz" # Select the speech-language, here en means English lang = 'en' #create the speech object #via using gTTS method. speech_obj = gTTS(text=text, lang=lang, slow=False) # save the speech as a .mp3 file speech_obj.save("speech.mp3") # use the os module to play the file os.system("speech.mp3")
Behind the Code
- Import Library gTTS and os to the python script.
- The text variable holds the text which is supposed to speech.
- The lang variable holds the value of language type, here we have used the English language that’s why we have used the “en” specifier.
- The speech_obj hold the value of gTTS() object where we passed our text and specified language.
- Using the gTTS save() method, we saved the speech_obj as a speech.mp3 file.
- at last with the help of the os module and system method we played the saved speech.mp3 file
- The mp3 file will be saved at the same location where the script is.
Here you learned how you could use python and its third-party library gTTS to covert a python string to speech. There are many other python libraries which can be used to create the same program, but we recommend to use gTTS because it is developed by google and much faster than other speech libraries such as pyttsx (Python Text to Speech).
If you like this article or have any suggestions, please let us know by commenting down below.