Text to Speech in Python

vegaseat 1 Tallied Votes 2K Views Share

Yes, you can let your computer read text to you. The task is relatively easy, if you have Windows on your machine. All you need is Microsoft's speech-API SAPI, the Python Text to Speech module pyTTS, and an updated version of win32com, all free downloads. Here are some experiments with the pyTTS module that literally talk to you.

# Text To Speech using SAPI (Windows) and Python module pyTTS by Peter Parente
# download installer file pyTTS-3.0.win32-py2.4.exe  
# from:  http://sourceforge.net/projects/uncassist
# also needs: http://www.cs.unc.edu/Research/assist/packages/SAPI5SpeechInstaller.msi
# and pywin32-204.win32-py2.4.exe at this date the latest version of win32com
# from: http://sourceforge.net/projects/pywin32/
# tested with Python24 on a Windows XP computer   vagaseat   15jun2005

import pyTTS
import time

tts = pyTTS.Create()

# set the speech rate, higher value = faster
# just for fun try values of -10 to 10
tts.Rate = 1
print "Speech rate =", tts.Rate

# set the speech volume percentage (0-100%)
tts.Volume = 90
print "Speech volume =", tts.Volume

# get a list of all the available voices
print "List of voices =", tts.GetVoiceNames()

# explicitly set a voice
tts.SetVoiceByName('MSMary')
print "Voice is set ot MSMary"

print

# announce the date and time, does a good job
timeStr = "The date and time is " + time.asctime()
print timeStr
tts.Speak(timeStr)

print

str1 = """
A young executive was leaving the office at 6 pm when he found 
the CEO standing in front of a shredder with a piece of paper in hand. 

"Listen," said the CEO, "this is important, and my secretary has left. 
Can you make this thing work?"

"Certainly," said the young executive. He turned the machine on, 
inserted the paper, and pressed the start button.

"Excellent, excellent!" said the CEO as his paper disappeared inside 
the machine. "I just need one copy."
"""
print str1
tts.Speak(str1)
tts.Speak('Haah haa haah haa')

print

str2 = """
Finagle's fourth law:
  Once a job is fouled up, anything done to improve it only makes it worse.
"""
print str2
print
print "The spoken text above has been written to a wave file (.wav)"
tts.SpeakToWave('Finagle4.wav', str2)

print "The wave file is loaded back and spoken ..."
tts.SpeakFromWave('Finagle4.wav')

print

print "Substitute a hard to pronounce word like Ctrl key ..."
#create an instance of the pronunciation corrector
p = pyTTS.Pronounce()
# replace words that are hard to pronounce with something that 
# is spelled out or misspelled, but at least sounds like it
p.AddMisspelled('Ctrl', 'Control')
str3 = p.Correct('Please press the Ctrl key!')
tts.Speak(str3)

print

print "2 * 3 = 6"
tts.Speak('2 * 3 = 6')

print

tts.Speak("sounds goofy, let's replace * with times")
print "Substitute * with times"
# ' * ' needs the spaces
p.AddMisspelled(' * ', 'times')
str4 = p.Correct('2 * 3 = 6')
tts.Speak(str4)

print

print "Say that real fast a few times!"
str5 = "The sinking steamer sunk!"
tts.Rate = 3
for k in range(7):
    print str5
    tts.Speak(str5)
    time.sleep(0.3)

tts.Rate = 0
tts.Speak("Wow, not one mispronounced word!")
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Can be shaped into a very helpful tool for any blind person you know!

The Geeky Kid 0 Junior Poster in Training

impressive...*has never used python b4* i like it alot!:cheesy:

so...do i just paste this into python interactive window?

The Geeky Kid 0 Junior Poster in Training

I typed it in, got no errors, but no sound or verification that it worked either...

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Generally you would copy and paste the code into a Python editor like IDLE or DrPython and then run it from there. The sound does use the sound chip and external speaker system of the PC.

personx121232 0 Newbie Poster

hello there dude...very good tutorial and inspiring for more projects using pyTTS...but i have a problem...when typing pyTTS.Create() , a ValueError exception is raised : "SAPI not supported" , i have windows xp sp2 , python 2.5 , and installed the first two packages you listed , plus microsoft voices , but still get the same error message , what do you recommend ?

bumsfeld 413 Nearly a Posting Virtuoso

A somewhat updated version:

''' pyttsx_speech_test102.py

speaks the present time in English, for instance
"Sat Apr 27 15:10:59"
as
"Saturday April 27th 15 hours 10 minutes and 59 seconds" 

download
pyttsx-1.1.tar.gz
from
https://pypi.python.org/pypi/pyttsx

also needs
setuptools-0.6c11.win32-py2.7.exe
from
https://pypi.python.org/pypi/setuptools#files


install setuptools first then
unpack the tar file and run (using Python27)
setup.py install
it installs 
C:\Python27\Lib\site-packages\pyttsx-1.1-py2.7.egg

info at
https://github.com/parente/pyttsx
and
http://pyttsx.readthedocs.org/en/v1.1/engine.html#examples

tested with Python273 in Windows7 (has the SAPI speech engine installed)
'''

import pyttsx
import time

engine = pyttsx.init()
# speech rate in words per minute
engine.setProperty('rate', 100)
# volume 0.0 to 1.0
engine.setProperty('volume', 0.9)

now_tuple = time.localtime()
#print(now_tuple)  # test

# slice off the year
now_str = time.asctime(now_tuple)[:-4]
print(now_str)  # test

engine.say(now_str)

engine.runAndWait()
ZZucker 342 Practically a Master Poster

Thanks Henri, module pyttsx is somewhat complicated to install, but works well.
Doesn't seem to work with Python3?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.