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行就能实现,但是中文大家自己做测试,效果很差。然后还有很多功能就不说了,大家可以自己访问包名的官网,去看看别人写的使用文档来查看参数。



猜你喜欢

转载自blog.csdn.net/m0_37713821/article/details/80488191