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, ... ?
You should try using the PyTTSx package since PyTTS is outdated. PyTTSx works with Python 2. For Python 3, install the PyTTSx3 package.
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'")
say
command to block my Python code, so I add an ampersand like this: os.system("say 'hello world' &")
–
Micmac spd-say
–
Asuncionasunder You should try using the PyTTSx package since PyTTS is outdated. PyTTSx works with Python 2. For Python 3, install the PyTTSx3 package.
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")
pip install pypiwin32
as administartor. –
Kura 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:
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();
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
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.
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.
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:
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.
© 2022 - 2024 — McMap. All rights reserved.