Install Gtts For Python In Mac

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

  1. Gtts Python Install
  2. Install Gtts
  3. Install Gtts For Python In Mac Free
  4. Install Gtts For Python In Mac 10
  5. Install Gtts For Python In Mac Os

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?

Import gTTS library and “os” module in order to play the converted audio. From gtts import gTTS import os. Creating a text that we want to convert into audio. Text = “Global warming is the long-term rise in the average temperature of the Earth’s climate system” gTTS supports multiple languages. Please refer to the documentation here. To get started, let's install required modules: pip3 install gTTS pyttsx3 playsound Online Synthesis. As you may guess, gTTS stands for Google Text To Speech, it is a Python library that wraps the original API to ease the work for us. Open up a new Python file and import: import gtts from playsound import playsound. Doing it Right¶. Let’s install a real version of Python. Before installing Python, you’ll need to install GCC. GCC can be obtained by downloading Xcode, the smaller Command Line Tools (must have an Apple account) or the even smaller OSX-GCC-Installer package. Installing Cython¶. Many scientific Python distributions, such as Anaconda, Enthought Canopy, and Sage, bundle Cython and no setup is needed.Note however that if your distribution ships a version of Cython which is too old you can still use the instructions below to update Cython.

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

For

On Windows

Install

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.

Install gtts for python in mac 2017

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.

The best of me full movie gomovies. 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

Install Gtts For Python In Mac

Gtts Python Install

Quick project

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

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.

Fomm crash on login. 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.

Gtts

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.

Install Gtts

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.

Install Gtts For Python In Mac Free

The last step is to play the media object.

Complete Code Listing

Install Gtts For Python In Mac 10

For Linux users, you will need to run the code from the Terminal using sudo. Windows users can run from their Python editor. Acoustic alchemy american english rapidshare downloads.

There we go!

Install Gtts For Python In Mac Os

So that was gTTS an interesting tool for your Python toolkit