Python Text to Speech

Posted in /  

Python Text to Speech
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    Python has thousands of libraries and APIs that developers can use to build sophisticated programs with ease. Here in this article, we will discuss how to write a Python program that can convert text to speech. We can build the program from scratch using Python core code, but that would be a heck of a task that takes too much time. So, here we will use a Python API  named 'gTTS', which will serve our purpose and allow us to create the program within 10 to 12 lines of code. Now, without further ado, let's get started!

    Prerequisites to Build the Python Text to Speech Program

    Following are three prerequisites:

    • Python (latest version)
    • pip
    • gTTS (latest version)

    Install Python

    Although you can use any text editor to write the Python script, you will need a Python interpreter on your system to execute that script. So, you need to download the latest version of Python. To install Python, you can visit its official website and download the appropriate Python executable file for your system, for more detail on how to install Python click here .

    pip

    pip is the Python package management tool that can be used to install and uninstall various Python packages registered in PyPI. Also, pip is a command-line tool, which means that you can install and manage all Python packages using a command-line interface, such as Command Prompt (for Windows) and terminal (for macOS and Linux). If you have installed the latest version of Python, then you do not need to install pip separately because it comes integrated with Python. However, if you are using older versions of Python, you might need to update or install pip separately. However, 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

    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. For Python, gTTS acts as a library and a CLI tool with Google translation API.  Using gTTS we can convert any text to a .mp3 file, and then by using an appropriate program we can play that file. gTTS supports multiple languages such as English, Hindi, Banga, 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. Following is the pip command that you need to run in your command-line interface :

    pip install gTTS

    After typing the above command, simply hit the Enter button. This will install the latest version of the gTTS package on your system.

    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 play 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 gTTS and os libraries to the Python script.
    • The text variable holds the text that we need to convert into 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 holds the value of the 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.
    • Lastly, 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 as that of the Python script.

    Conclusion

    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 that can be used to create the same program, but we recommend using gTTS because it is developed by Google and is 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.

    People are also reading:

    Leave a Comment on this Post

    0 Comments