python 利用pyttsx3文字转语音 适用于macOS windows树莓派

2019-04-04 by 崔斐然

python 利用pyttsx3文字转语音

更新于2019-05-08:

树莓派安装pyodbc失败参考这里:
安装 unixodbc-dev,

sudo apt install unixodbc-dev

https://github.com/mkleehammer/pyodbc/wiki/Install#centos-7

踩坑:

报错

>>> engine = pyttsx3.init()
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyttsx3/__init__.py", line 44, in init
    eng = _activeEngines[driverName]
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/weakref.py", line 137, in __getitem__
    o = self.data[key]()
KeyError: None

python 利用pyttsx3文字转语音 适用于macOS windows树莓派_第1张图片

解决办法:

安装 pyobjc
pip install pyobjc

或者使用python2 这个方法我没试过,因为安装pyobjc就已经解决问题了
python 利用pyttsx3文字转语音 适用于macOS windows树莓派_第2张图片

解决办法来源:https://github.com/nateshmbhat/pyttsx3/issues/1
适用于macOS,当前10.14.2可用


正式教程

安装:

pip install pyttsx3

anaconde下如何安装第三方包详见另一篇博文:

使用方法:

朗读文字

import pyttsx3
engine = pyttsx3.init()
engine.say('Sally sells seashells by the seashore.')
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

文字过长时打断发音

import pyttsx3
def onWord(name, location, length):
  print('word', name, location, length)
  if location > 10:
    engine.stop()
 
engine = pyttsx3.init()
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

使用真人发音

engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
  engine.setProperty('voice', voice.id)
  engine.say('The quick brown fox jumped over the lazy dog.')
 
engine.runAndWait()

语速控制

engine = pyttsx3.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', rate+50)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

音量控制

engine = pyttsx3.init()
volume = engine.getProperty('volume')
engine.setProperty('volume', volume-0.25)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

语音引擎参数解释

    类似于设计模式中的“工厂模式”,pyttsx3通过初始化来获取语音引擎。当我们第一次调用init操作的时候,会返回一个pyttsx3的engine对象,再次调用的时候,如果存在engine对象实例,就会使用现有的,否则再重新创建一个。

pyttsx.init([driverName : string, debug : bool]) → pyttsx.Engine

    从方法声明上来看,第一个参数指定的是语音驱动的名称,这个在底层适合操作系统密切相关的。如下:
drivename:由pyttsx3.driver模块根据操作系统类型来调用,默认使用当前操作系统可以使用的最好的驱动
sapi5 - SAPI5 on Windows
nsss - NSSpeechSynthesizer on Mac OS X
espeak - eSpeak on every other platform
debug: 这第二个参数是指定要不要以调试状态输出,建议开发阶段设置为True

引擎接口

方法签名 参数列表 返回值 简单释义
connect(topic : string, cb : callable)  topic:要描述的事件名称;cb:回调函数  →   dict  在给定的topic上添加回调通知
disconnect(token : dict)  token:回调失联的返回标记  Void 结束连接
endLoop() None → None  简单来说就是结束事件循环
getProperty(name : string)  name有这些枚举值“rate, vioce,vioces,volumn  → object  获取当前引擎实例的属性值
setProperty(name : string)  name有这些枚举值“rate, vioce,vioces,volumn → object  设置当前引擎实例的属性值
say(text : unicode, name : string)  text:要进行朗读的文本数据; name: 关联发音人,一般用不到 → None 预设要朗读的文本数据,这也是“万事俱备,只欠东风”中的“万事俱备”
runAndWait() None → None  这个方法就是“东风”了。当事件队列中事件全部清空的时候返回
startLoop([useDriverLoop : bool])  useDriverLoop:是否启用驱动循环 → None  开启事件队列

元数据音调在pyttsx3.voice.Voice中,处理合成器的发音。

age 发音人的年龄,默认为None

gender 以字符串为类型的发音人性别: male, female, or neutral.默认为None

id 关于Voice的字符串确认信息. 通过 pyttsx3.engine.Engine.setPropertyValue()来设置活动发音签名. 这个属性总是被定义。

languages 发音支持的语言列表,如果没有,则为一个空的列表。

name 发音人名称,默认为None.

你可能感兴趣的:(Python,python朗读文字,pyttsx)