Install Gtts For Python In Mac



Mac OS X 10.8 comes with Python 2.7 pre-installed by Apple. If you wish, you are invited to install the most recent version of Python from the Python website (A current “universal binary” build of Python, which runs natively on the Mac’s new Intel and legacy PPC CPU’s, is available there. GTTS-token (Google Text to Speech token): A python implementation of the token validation of Google Translate. Install pip install gTTS-token Description. Google Translate requires a tk param when making a request to its translate API. This project provides an implementation for that algorithm in Python.

Another Tuesday and another tool to add to your Python toolkit.

This time we take a look at a Python library that can provide exceptional text to speech. I used this library in Linux Format issue 222 to create an 'Information Butler' powered by Raspberry Pi, which retrieved weather and news headlines from BBC news just by waving my hand over the box.

So what is it?

gTTS, Google Text-To-Speech is a Python library from Pierre-Nick Durette that converts a string into an audio file. It does not automatically speak, rather it creates an mp3 file that we can then play.

So how can I install it?

On Mac and Linux

On Windows

You will also need to install the VLC app, which can be found via their website for Windows users. but for Linux users it is best to install using your package manager / software centre.

So how can I use it?

The most basic test is to say hello world! So in your favourite Python3 editor, write the following code.

This code will import the gTTS library, then we create an object called tts which will store the string that we wish to 'speak'. The final line saves the string as an audio file called hello_world.mp3 in the same directory as where the code is ran.

Is that it?

Well no as we can also provide an argument when using the library, that will change the accent and language of the speaker. Note: This does not translate the text from say English to French, rather you get a French voice reading English text. But it is different for numbers, for example the time, as it will read the text in the chosen language.

To use another accent / language, we pass the language as an argument when calling the object.

So before we did this

But to have the text read in an American accent we use

Quick project

This is a remix of my inputs blog post and of my VLC for Python post.

Install Gtts For Python In Mac 2

Python

Note: The input library requires running as root / sudo on Linux as it needs to access the keyboard directly. So I ran the code in a terminal, rather than the IDLE Python shell.

The goal of the project is to speak the time when the user presses the space bar. So let's start by importing the libraries needed.

  • gTTS for the Google Text-To-Speech.
  • get_key from inputs for detecting when a key is pressed.
  • gmtime, strftime to get the current time and format so that we can read it.
  • vlc to play the audio file.

To continuously run the code we need a loop, and for this basic test while True will do the job. The first line of code in the loop will store the key presses into a variable called events

Still inside the loop and we next create a variable called current_time which stores the current time, but only the Hours and Minutes %H:%M.

So now we use a for loop to check for a key press event, i.e we press a key! If the event is KEY_SPACE which is how inputs refers to the space bar, and the event.state 1 which means the key has been pressed, then it activates the code indented below it.

So inside the for loop and we start by printing the current time to the Python shell, for debug.

Then we use gTTS to read the current time in a UK voice / accent (in the video I change it to American, UK, Italian and French.)

Then I save the audio to my home directory.

The next step is to tell VLC where to the audio file is, and we save that information to an object called media.

The last step is to play the media object.

Complete Code Listing

Install Gtts For Python In Mac Download

For Linux users, you will need to run the code from the Terminal using sudo. Windows users can run from their Python editor.

Install Gtts For Python In Machine Learning

There we go!

So that was gTTS an interesting tool for your Python toolkit