ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python将文本转换成语音的代码

2021-11-04 16:00:06  阅读:185  来源: 互联网

标签:转换成 set python tts time print 文本 pyTTS Speak


将写代码过程中经常用的一些代码片段备份一下,如下代码段是关于python将文本转换成语音的代码,应该是对小伙伴们有一些好处。
# Text To Speech using SAPI (Windows) and Python module pyTTS by Peter Parente
# download installer file pyTTS-3.0.win32-py2.4.exe
# and pywin32-204.win32-py2.4.exe at this date the latest version of win32com
# 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

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





 

标签:转换成,set,python,tts,time,print,文本,pyTTS,Speak
来源: https://www.cnblogs.com/odsxe/p/15508535.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有