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)
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
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.
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.
install pip install pypiwin32
from win32com.client import Dispatch
speak = Dispatch("SAPI.SpVoice")
speak.Speak("Ciao")
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")
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.
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'")