Google Text to Speech API: how to create mp3 with text in any language

Thanks to a Google API, we can create audio from a text, in any language.  It just takes a couple of seconds and the game is done with Python. Let’s see how we can do it.

Installation: pip install gtts

First of all you have to install the gtts module. Open the cmd and write:

pip install gtts

If you installed Python, you should have pip, otherwise, uninstall it and reinstall it again.

Now you can start your script with your editor writing

from gtts import gTTS

The gTTS (class?) is all we need to create our mp3 file starting from a text. Let’s move on.

The shortest way is the better

Let’s create our speech, righ now:

from gtts import gTTS

speech = gTTS("Hello World", lang="en")
speech.save("hello.mp3")

We passed to the function the text and the language (english) and then we also saved the file.

In the following video I explain how easy is to do it.

Video: Python and the gtts module


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.