使用pyttsx的python包,你可以将文本转换为语音。
安装命令
pip install pyttsx3 -i https://pypi.tuna.tsinghua.edu.cn/simple
运行一个简单的语音 ‘大家好’。
import pyttsx3 as pyttsx
engine = pyttsx.init() #初始化
engine.say('大家好')
engine.runAndWait()
另一种文本转语音方法。
from win32com.client import Dispatch
speaker = Dispatch('SAPI.SpVoice') #创建Dispatch对象
speaker.Speak('大家好') #调用Speak方法
del speaker #释放
这种方法可能会报错,
ImportError: DLL load failed while importing win32api: 找不到指定的模块。
网站下载与自己安装的 “Python" 版本相适应的 "pywin32" 安装程序。
使用SpeechLib完成文本转换语言
from comtypes.client import CreateObject
from comtypes.gen import SpeechLib
engine = CreateObject('SAPI.SpVoice') #调用方法
stream = CreateObject('SAPI.SpFileStream') #输出到目标对象的流
infile = '1.txt' #要读取的文本
outfile &#