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)
There are a number of ways to make Python speak in both Python3 and Python2, two great methods are:
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
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! :)
Pyttsx3 is a python module which is a modern clone of pyttsx, modified to work perfectly well in the latest versions of Python 3!
It is 100% MULTI-PLATFORM and WORKS OFFLINE and IS ACTIVE/STILL BEING DEVELOPED and WORKS WITH ANY PYTHON VERSION
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();
This is the best multi platform option!
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/
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
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?
I prefer to use the Google Text To Speech library because it has a more natural voice.
from gtts import gTTS
def speak(text):
tts = gTTS(text=text, lang="en")
filename = "voice.mp3"
tts.save(filename)
There is one limitation. gTTS can only convert text to speech and save. So you will have to find another module or function to play that file. (Ex: playsound)
Playsound is a very simple module that has one function, which is to play sound.
import playsound
def play(filename):
playsound.playsound(filename)
You can call playsound.playsound() directly after saving the mp3 file.