网页设计模板素材图片旅游/太原seo代理商
python3.8现在有很多坑存在,很多module要么没有对应版本的安装包,要么安装会遇到多问题。这次使用pyttsx3的时候,安装没问题,但是代码运行就一直报错。
Traceback (most recent call last):
File "Python38\lib\site-packages\pyttsx3\__init__.py", line 44, in init
eng = _activeEngines[driverName]
File "Python38\lib\weakref.py", line 137, in __getitem__
o = self.data[key]()
KeyError: None
1
2
3
4
5
6
经过排查,确认电脑的语音识别是没问题的,多次重装安装也是没法解决,然后找其他解决方法,最终找到原因是版本匹配上有问题。
解决方法:指定pyttsx3的版本为2.71(如果指定为2.80是有问题的,安装失败)
pip install pyttsx3==2.71
1
当然,如果不是非要用pyttsx3或者最终没解决的,可以用pythoncom
import pythoncom
from win32com import client
pythoncom.CoInitialize()
engine=client.Dispatch("SAPI.SpVoice")
engine.Speak('hello,你好呀')
一些设置参数
def use_pyttsx3():
# 创建对象
engine = pyttsx3.init()
# 获取当前语音速率
rate = engine.getProperty('rate')
print(f'语音速率:{rate}')
# 设置新的语音速率
engine.setProperty('rate', 200)
# 获取当前语音音量
volume = engine.getProperty('volume')
print(f'语音音量:{volume}')
# 设置新的语音音量,音量最小为 0,最大为 1
engine.setProperty('volume', 1.0)
# 获取当前语音声音的详细信息
voices = engine.getProperty('voices')
print(f'语音声音详细信息:{voices}')
# 设置当前语音声音为女性,当前声音不能读中文
engine.setProperty('voice', voices[1].id)
# 设置当前语音声音为男性,当前声音可以读中文
engine.setProperty('voice', voices[0].id)
# 获取当前语音声音
voice = engine.getProperty('voice')
print(f'语音声音:{voice}')
# 语音文本
path = 'test.txt'
with open(path, encoding='utf-8') as f_name:
words = str(f_name.readlines()).replace(r'\n', '')
# 将语音文本说出来
engine.say(words)
engine.runAndWait()
engine.stop()
if __name__ == '__main__':
use_pyttsx3()
原文链接:https://blog.csdn.net/u012902518/article/details/106881002