Question

How could I make Python say some text?

I could use Festival with subprocess but I won't be able to control it (or maybe in interactive mode, but it won't be clean).

Is there a Python TTS library? Like an API for Festival, eSpeak, ... ?

Was it helpful?

Solution

Please note that this only work with python 2.x

You should try using the PyTTSx package since PyTTS is outdated. PyTTSx works with the lastest python version.

http://pypi.python.org/pypi/pyttsx/1.0 -> The package

Hope it helps

OTHER TIPS

A bit cheesy, but if you use a mac you can pass a terminal command to the console from python.

Try typing the following in the terminal:

$ say 'hello world' 

And there will be a voice from the mac that will speak that. From python such a thing is relatively easy:

import os
os.system("echo 'hello world'")
os.system("say 'hello world'") 

How to hear the voice of pc on windows

from win32com.client import Dispatch

speak = Dispatch("SAPI.SpVoice")

speak.Speak("Ciao")

Using google text-to-speech Api to create an mp3 and here it

After you installed the gtts module in cmd: pip install gtts

from gtts import gTTS
import os    

tts = gTTS(text="This is the pc speaking", lang='en')
tts.save("pcvoice.mp3")
# to start the file from python
os.system("start pcvoice.mp3")

The python-espeak package is available in Debian, Ubuntu, Redhat, and other Linux distributions. It has recent updates, and works fine.

from espeak import espeak
espeak.synth("Hello world.")

Jonathan Leaders notes that it also works on Windows, and you can install the mbrola voices as well. See the espeak website at http://espeak.sourceforge.net

A simple Google led me to pyTTS, and a few documents about it. It looks unmaintained and specific to Microsoft's speech engine, however.

On at least Mac OS X, you can use subprocess to call out to the say command, which is quite fun for messing with your coworkers but might not be terribly useful for your needs.

It sounds like Festival has a few public APIs, too:

Festival offers a BSD socket-based interface. This allows Festival to run as a server and allow client programs to access it. Basically the server offers a new command interpreter for each client that attaches to it. The server is forked for each client but this is much faster than having to wait for a Festival process to start from scratch. Also the server can run on a bigger machine, offering much faster synthesis. linky

There's also a full-featured C++ API, which you might be able to make a Python module out of (it's fun!). Festival also offers a pared-down C API -- keep scrolling in that document -- which you might be able to throw ctypes at for a one-off.

Perhaps you've identified a hole in the market?

There are a number of ways to make Python speak in both Python3 and Python2, two great methods are:

  • Using os

If you are on mac you will have the os module built into your computer. You can import the os module using:

import os

You can then use os to run terminal commands using the os.system command:

os.system("Your terminal")

In terminal, the way you make your computer speak is using the "say" command, thus to make the computer speak you simply use:

os.system("say 'some text'")

If you want to use this to speak a variable you can use:

os.system("say " + myVariable)

The second way to get python to speak is to use

  • The pyttsx module

You will have to install this using

pip isntall pyttsx3

or for Python3

pip3 install pyttsx3

You can then use the following code to get it to speak:

import pyttsx3
engine = pyttsx3.init()

engine.say("Your Text")

engine.runAndWait()

I hope this helps! :)

There may not be anything 'Python specific', but the KDE and GNOME desktops offer text-to-speech as a part of their accessibility support, and also offer python library bindings. It may be possible to use the python bindings to control the desktop libraries for text to speech.

If using the Jython implementation of Python on the JVM, the FreeTTS system may be usable.

Finally, OSX and Windows have native APIs for text to speech. It may be possible to use these from python via ctypes or other mechanisms such as COM.

You can use espeak using python for text to speech converter.
Here is an example python code

    from subprocess import call
    speech="Hello World!"
    call(["espeak",speech])

P.S : if espeak isn't installed on your linux system then you need to install it first.
Open terminal(using ctrl + alt + T) and type

    sudo apt install espeak

If you are using python 3 and windows 10, the best solution that I found to be working is from Giovanni Gianni. This played for me in the male voice:

import win32com.client as wincl
speak = wincl.Dispatch("SAPI.SpVoice")
speak.Speak("This is the pc voice speaking")

I also found this video on youtube so if you really want to, you can get someone you know and make your own DIY tts voice.

This is what you are looking for. A complete TTS solution for the Mac. You can use this standalone or as a co-location Mac server for web apps:

http://wolfpaulus.com/jounal/mac/ttsserver/

PYTTSX3!

THE COMPLETE SOLUTION TO ALL YOU PYTHON TEXT-TO-SPEECH NEEDS

WHAT:

Pyttsx3 is a python module which is a clone of the pyttsx module for Python 2.x, except modified to work perfectly well in the latest versions of Python 3!

WHY:

It is 100% MULTI-PLATFORM AND WORKS OFFLINE AND IS ACTIVE/STILL BEING DEVELOPED AND WORKS WITH ANY PYTHON VERSION

HOW:

It can be easily installed with pip install pyttsx3 and usage is the same as pyttsx:

import pyttsx3;
engine = pyttsx3.init();
engine.say("I will speak this text");
engine.runAndWait();

CONCLUSION:

It works easily and flawlessly, so I think this is the best solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top