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, ... ?
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, ... ?
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 install 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! :)
What:
Pyttsx3 is a python module which is a modern clone of pyttsx, modified to work with the latest versions of Python 3!
Why:
It is multi-platform, works offline, and works with any python version.
How:
It can be 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();
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.
Combining the following sources, the following code works on Windows, Linux and macOS using just the platform and os modules:
tx = input("Text to say >>> ")
tx = repr(tx)
import os
import platform
syst = platform.system()
if syst == 'Linux' and platform.linux_distribution()[0] == "Ubuntu":
os.system('spd-say %s' % tx)
elif syst == 'Windows':
os.system('PowerShell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak(%s);"' % tx)
elif syst == 'Darwin':
os.system('say %s' % tx)
else:
raise RuntimeError("Operating System '%s' is not supported" % syst)
Note: This method is not secure and could be exploited by malicious text.
Just use this simple code in python.
Works only for windows OS.
from win32com.client import Dispatch
def speak(text):
speak = Dispatch("SAPI.Spvoice")
speak.Speak(text)
speak("How are you dugres?")
I personally use this.