SoFunction
Updated on 2024-12-19

A python implementation of converting text to speech

This article example describes the method of python to convert text to speech. Shared for your reference. Specific implementation methods are as follows:

# Text To Speech using SAPI (Windows) and Python module pyTTS by Peter Parente
# download installer file pyTTS-3.0.win32-py2. 
# from: /projects/uncassist
# also needs: /Research/assist/packages/
# and pywin32-204.win32-py2. at this date the latest version of win32com
# from: /projects/pywin32/
# tested with Python24 on a Windows XP computer  vagaseat  15jun2005
import pyTTS
import time
tts = ()
# set the speech rate, higher value = faster
# just for fun try values of -10 to 10
 = 1
print "Speech rate =", 
# set the speech volume percentage (0-100%)
 = 90
print "Speech volume =", 
# get a list of all the available voices
print "List of voices =", ()
# explicitly set a voice
('MSMary')
print "Voice is set ot MSMary"
print
# announce the date and time, does a good job
timeStr = "The date and time is " + ()
print timeStr
(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
(str1)
('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)"
('', str2)
print "The wave file is loaded back and spoken ..."
('')
print
print "Substitute a hard to pronounce word like Ctrl key ..."
#create an instance of the pronunciation corrector
p = ()
# replace words that are hard to pronounce with something that 
# is spelled out or misspelled, but at least sounds like it
('Ctrl', 'Control')
str3 = ('Please press the Ctrl key!')
(str3)
print
print "2 * 3 = 6"
('2 * 3 = 6')
print
("sounds goofy, let's replace * with times")
print "Substitute * with times"
# ' * ' needs the spaces
(' * ', 'times')
str4 = ('2 * 3 = 6')
(str4)
print
print "Say that real fast a few times!"
str5 = "The sinking steamer sunk!"
 = 3
for k in range(7):
  print str5
  (str5)
  (0.3)
 = 0
("Wow, not one mispronounced word!")

I hope that what I have said in this article will help you in your Python programming.