关于pyttsx3
ptyysx3是一个将文字转换为语音的三方库,和其他的库不同的是,它可以离线使用。并且支持男声女声,修改声音速率,音量大小等。
安装
pip install pyttsx3 复制代码
使用
普通文本
import pyttsx3 engine = pyttsx3.init() engine.say('Sally sells seashells by the seashore.') engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait() 复制代码
将语音保存到文件
import pyttsx3 engine = pyttsx3.init() engine.save_to_file('Hello World' , 'test.mp3') engine.runAndWait() 复制代码
侦听事件
import pyttsx3 def onStart(name): print 'starting', name def onWord(name, location, length): print 'word', name, location, length def onEnd(name, completed): print 'finishing', name, completed engine = pyttsx3.init() engine.connect('started-utterance', onStart) engine.connect('started-word', onWord) engine.connect('finished-utterance', onEnd) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait() 复制代码
打断话语
import pyttsx3 def onWord(name, location, length): print 'word', name, location, length if location > 10: engine.stop() engine = pyttsx3.init() engine.connect('started-word', onWord) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait() 复制代码
改变声音
engine = pyttsx3.init() voices = engine.getProperty('voices') for voice in voices: engine.setProperty('voice', voice.id) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait() 复制代码
更改语音速率
engine = pyttsx3.init() rate = engine.getProperty('rate') engine.setProperty('rate', rate+50) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait() 复制代码
更改音量
engine = pyttsx3.init() volume = engine.getProperty('volume') engine.setProperty('volume', volume-0.25) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait() 复制代码
运行驱动程序事件循环
engine = pyttsx3.init() def onStart(name): print 'starting', name def onWord(name, location, length): print 'word', name, location, length def onEnd(name, completed): print 'finishing', name, completed if name == 'fox': engine.say('What a lazy dog!', 'dog') elif name == 'dog': engine.endLoop() engine = pyttsx3.init() engine.connect('started-utterance', onStart) engine.connect('started-word', onWord) engine.connect('finished-utterance', onEnd) engine.say('The quick brown fox jumped over the lazy dog.', 'fox') engine.startLoop() 复制代码
使用外部事件循环
engine = pyttsx3.init() engine.say('The quick brown fox jumped over the lazy dog.', 'fox') engine.startLoop(False) # engine.iterate() must be called inside externalLoop() externalLoop() engine.endLoop() 复制代码
你不妨动手试试,个人感觉还是很好玩的。