How to Change Voices in Gtts Python

The ability to change voices in Google’s Text-to-Speech service available in Python, abbreviated as gTTS, offers a wide range of possibilities for the development of applications that require speech synthesis, like IVR systems, readability of texts, voice commands, and much more.

In this tutorial, we’ll walk through the simple process of changing voices in gTTS using Python. All you need is basic knowledge of Python and gTTS installed in your working environment.

Step 1: Install gTTS

If you haven’t already, the first step is to install the gTTS library in your Python environment. You can use pip, the Python package installer, for this. Fire up your terminal and type the following command:

Step 2: Import the library

Once you’ve successfully installed gTTS, you need to import it into your Python script. Remember to also import the os module, since we’ll need it to play the audio file that gTTS creates.

Step 3: Use gTTS to convert text to speech

Now, we’ll use gTTS to convert text to speech. This is done by creating a gTTS object and calling the save method on it to save the resultant speech audio into a file.

The above code takes a string as input and converts it into speech. The speech is then saved as an MP3 audio file. Play the file to listen to the speech.

Note: gTTS supports multiple languages, which you can specify when creating a gTTS object. The default is English. You can find the full language list on the official gTTS documentation.

Step 4: Changing the Voice & Language

In gTTS, the voice is tied to the language. Therefore, to change voices, you actually have to change the language or, more specifically, the dialect. Here’s an example of transforming text to speech in French:

Here, the ‘lang’ parameter is used to specify the language of the text to be converted into speech.

Full Code

Conclusion

As you have seen, using gTTS to change voices by switching languages or dialects in Python is straightforward. You can now incorporate this feature into your own Python applications! Keep improving and experimenting, and let the world hear your applications’ voices!