来源:CSDN
发布时间:Nov 15, 2020, 1:55:55 PM
原地址:https://blog.csdn.net/weixin_42683461/article/details/109704759
一、环境准备:
安装python 2.7
安装pyttsx:
pip install pyttsx
二、脚本
import os
import sys
import pyttsx
import random
import time
from time import sleep
class TextToVoice(object):
def commands(self):
engine = pyttsx.init()
voices = engine.getProperty("voices")
#设置声音输出类型
i = random.randint(0, 1)
engine.setProperty("voice", voices[i].id)
#设置声音输出频率
engine.setProperty("rate", 200)
with open("Commands.txt", "r+") as f:
lines = f.readlines()
engine.runAndWait()
for line in lines[0:]:
engine.say("OK Google")
engine.runAndWait()
time.sleep(3)
engine.say(line)
engine.runAndWait()
sleep(30)
def main(self):
self.commands()
if __name__ == '__main__':
test = TextToVoice()
test.main()
三、问题
执行过程中直接停止,无任何提示,cmd中执行以下脚本,提示 No module named win32com.client
python
import pyttsx
engine = pyttsx.init()
解决方法:
访问:https://github.com/mhammond/pywin32/releases
根据安装的python版本及系统下载对应版本,如我的是python 2.7 + 64位系统,下载Release 228:
下载完成后傻瓜式安装,一直下一步即可
再次执行
engine = pyttsx.init()
engine.say("Hello World")
engine.runAndWait()
即可读出 Hello world