How to make Python speak
Asked Answered
F

14

85

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, ... ?

Fowler answered 23/10, 2009 at 15:2 Comment(2)
does "Festival" have a public API?Retro
For text to speech I found this package called "gTTS" in Python. You can try this out. It does work with Python 3.5. The github repo for this package is gTTS-github.Saraisaraiya
H
42

You should try using the PyTTSx package since PyTTS is outdated. PyTTSx works with Python 2. For Python 3, install the PyTTSx3 package.

http://pypi.python.org/pypi/pyttsx/

https://pypi.org/project/pyttsx3/

Hypnogenesis answered 1/12, 2009 at 17:30 Comment(5)
Does not work for python 3. This answer was up to date as of 2009Perspire
Despite being available through pip, still does not work as of 2015Tieshatieup
I confirm it does not work with python3 and easy fixes (printf as a function, fixing exception handling syntax and fixing imports) don't make it work, it simply fails silently. Interfacing with espeak (what it does on Linux) is as simple as spawning a subprocess, so that's what I ended up doing.Clara
Just added a comment eat the top of the question to note this only works with Python 2.xHypnogenesis
PYTTSX3 works in python 3 too. Its a cool moduleSlippy
S
43

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'") 
Springer answered 25/8, 2013 at 11:27 Comment(2)
I don't want the say command to block my Python code, so I add an ampersand like this: os.system("say 'hello world' &")Micmac
On ubuntu, the terminal command to use is spd-sayAsuncionasunder
H
42

You should try using the PyTTSx package since PyTTS is outdated. PyTTSx works with Python 2. For Python 3, install the PyTTSx3 package.

http://pypi.python.org/pypi/pyttsx/

https://pypi.org/project/pyttsx3/

Hypnogenesis answered 1/12, 2009 at 17:30 Comment(5)
Does not work for python 3. This answer was up to date as of 2009Perspire
Despite being available through pip, still does not work as of 2015Tieshatieup
I confirm it does not work with python3 and easy fixes (printf as a function, fixing exception handling syntax and fixing imports) don't make it work, it simply fails silently. Interfacing with espeak (what it does on Linux) is as simple as spawning a subprocess, so that's what I ended up doing.Clara
Just added a comment eat the top of the question to note this only works with Python 2.xHypnogenesis
PYTTSX3 works in python 3 too. Its a cool moduleSlippy
U
36

install pip install pypiwin32

How to use the text to speech features of a Windows PC

from win32com.client import Dispatch

speak = Dispatch("SAPI.SpVoice").Speak

speak("Ciao")

Using google text-to-speech Api to create an mp3 and hear 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")
Unskillful answered 30/10, 2016 at 8:33 Comment(6)
You can install required module in your system by running pip install pypiwin32 as administartor.Kura
Google solution seems to be one of the best : allows to change of language, it is also really fast.Barbaresi
Strangely, the first code example works on some Windows 10 PCs but not others. Why is that?Alaska
@Alaska I am not sure, but you should check in the control panel, the syntetized voice (I don't remember the exact name of this options) and see if it has been set... there is a button you can press to see if it works. If it works in the settings, should work with the code, because I think it uses the windows synthesized voice, I think.Unskillful
It's been set, but when the command is run through CMD it says "Access is denied."Alaska
did you installed pip install pypiwin32 as administrator (as suggested by Kamil Szot?)?Unskillful
S
16

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

Squish answered 7/6, 2014 at 19:0 Comment(0)
A
10

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?

Athlete answered 23/10, 2009 at 15:8 Comment(0)
C
9

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 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! :)

Cocaine answered 30/5, 2018 at 6:52 Comment(0)
F
7

PYTTSX3

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();
Flourish answered 6/5, 2018 at 2:48 Comment(2)
Is there a recommended way to make saying async?Defraud
@AnatolyAlekseev No there doesn't seem to be one. Just use asyncio or however you do that in python I guess.Flourish
S
3

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
Stedt answered 26/1, 2017 at 10:11 Comment(0)
B
3

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.

Barograph answered 15/10, 2020 at 8:36 Comment(0)
M
2

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.

Mccutcheon answered 23/10, 2009 at 15:19 Comment(0)
S
2

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.

Sacha answered 11/8, 2017 at 4:21 Comment(1)
Is there a way to get this to work with other languages (Japanese or Chinese?)Chuff
E
1

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/

Egin answered 5/7, 2014 at 22:3 Comment(0)
U
0

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.

Urumchi answered 30/11, 2019 at 17:13 Comment(0)
C
0

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.

Countermark answered 19/12, 2020 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.