2019-09-25 14:59:08 5893瀏覽
今天千鋒扣丁學堂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全棧開發免費公開課】