pyttsx3 快速上手之:语音合成播报

Python pyttsx3 快速上手之:语音合成播报

pyttsx3 是python中最常用的文字转语音库,使用方便,功能较为完整

安装 pyttsx3:

首先安装 pyttsx3 lib:

  • pip install pyttsx3

API封装

然后封装下pyttsx3 API,新建一个speaker.py 如下:
在这里插入图片描述

import pyttsx3

global __speak_engine
__speak_engine = None

def say(content):
	global __speak_engine
	if not __speak_engine:
		__speak_engine = pyttsx3.init()
		voices = __speak_engine.getProperty('voices')
		__speak_engine.setProperty('voice', voices[1].id)
		__speak_engine.setProperty('rate', 150)
		__speak_engine.setProperty('volume', 1)

	__speak_engine.say(content)
	__speak_engine.runAndWait()

其中Property的rate/voice/volume可以根据需要自行调整:

API使用

这样在使用中直接调用 say(…),就可以实现中英文语音播报了:

(python3可以直接调用播报中文)

使用举例:

在这里插入图片描述

from speaker import *


if __name__ == '__main__':
    say("Hello Howie Xue, we can speak by Python now")
	say("你好")


博主热门文章推荐:

一篇读懂系列:

LoRa Mesh系列:

网络安全系列:

嵌入式开发系列:

AI / 机器学习系列:


在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/HowieXue/article/details/120638556