python文字轉語音
釋出時間:2018-05-28 20:33,
浏覽次數:382
, 标簽:
python
你覺得将文字轉成語音需要寫多少行代碼才能完成?
我用了7行,你呢?
#coding:utf-8 import sys reload(sys) sys.setdefaultencoding("utf-8") import
pyttsx engine = pyttsx.init() engine.say('hello 雪桐') engine.runAndWait()
這不是8行嗎?哦,因為這是python2寫的,代碼中有中文需要加一行注釋,采用utf8編碼允許代碼中含有中文。
看這行代碼engine.say('hello 雪桐'),可以在括号中加入很多東西,突然有個大膽的想法。可以改發音人的聲音,發音速度嗎?
# coding:utf-8 import sys reload(sys) sys.setdefaultencoding("utf-8") import
pyttsx ''' engine = pyttsx.init() #engine.say('hello world. Sally sells
seashells by the seashore') engine.say('草泥馬') engine.runAndWait() #朗讀一次
engine.endLoop() ''' #發音人 ''' engine = pyttsx.init() voices =
engine.getProperty('voices') for voice in voices: engine.setProperty('voice',
voice.id) engine.say('你好呀') engine.runAndWait() ''' #語速控制 ''' engine =
pyttsx.init() rate = engine.getProperty('rate') engine.setProperty('rate',
rate+50) engine.say('the lazy dog.') engine.runAndWait() ''' #音量控制 ''' engine =
pyttsx.init() volume = engine.getProperty('volume')
engine.setProperty('volume', volume-0.25) engine.say('the lazy dog.')
engine.runAndWait() '''
還有一個微軟提供的api接口,不過試了一下,中文支援效果很差。
https://github.com/mhammond/pywin32/archive/master.zip
下載下傳好之後,把檔案夾名改成pywin32。然後把檔案夾放在下面代碼一個目錄下。
readtest.py代碼是:
import win32com.client speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak("Hello, it works!")
沒錯,3行就能實作,但是中文大家自己做測試,效果很差。然後還有很多功能就不說了,大家可以自己通路包名的官網,去看看别人寫的使用文檔來檢視參數。