2019-09-25 14:59:08 3387浏览
今天千锋扣丁学堂Python培训老师给大家分享一篇关于利用pyttsx3文字转语音过程详解,文中通过示例代码介绍的非常详细,下面我们一起来看一下吧。
# -*- coding: utf-8 -*-
import pyttsx3
engine = pyttsx3.init()
with open("all.txt",'r',encoding='utf-8') as f:
while 1:
line = f.readline()
print(line, end = '')
engine.say(line)
engine.runAndWait()
import pyttsx3
with open('all.txt','r',encoding='utf-8') as f:
line = f.read()#文件不大,一次性读取
engine = pyttsx3.init()
#调整频率
rate = engine.getProperty('rate')
engine.setProperty('rate', rate-50)
# 调整音量
volume = engine.getProperty('volume')
engine.setProperty('volume', volume+0.25)
engine.say(line)
engine.runAndWait()
pip install pyttsx3
pyttsx.init([driverName : string, debug : bool]) → pyttsx.Engine
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
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.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.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.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()
【关注微信公众号获取更多学习资料】 【扫码进入Python全栈开发免费公开课】