python将文本转换成语音的代码
2021/11/4 17:13:48
本文主要是介绍python将文本转换成语音的代码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
将写代码过程中经常用的一些代码片段备份一下,如下代码段是关于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"
# announce the date and time, does a good job
timeStr = "The date and time is " + time.asctime()
print timeStr
tts.Speak(timeStr)
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')
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 "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
tts.Speak(str4)
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!")
这篇关于python将文本转换成语音的代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-24Python编程基础详解
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程