pyttsx3 初识

pyttsx3

pyttsx3是Python中的文本到语音转换库。与其他库不同,它可以脱机工作,并且与Python 2和3兼容。

简单入门

安装

1
pip install pyttsx3

简单示例

1
2
3
4
#coding=utf-8
import pyttsx3
""" 语音播放 Hello World """
pyttsx3.speak("Hello World!")

或者

1
2
3
4
5
6
#coding=utf-8
import pyttsx3
""" 语音播放 Hello World """
engine = pyttsx3.init()
engine.say("Hello World!")
engine.runAndWait()

进阶示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import pyttsx3
engine = pyttsx3.init() # object creation
""" 把语音存储到文件 """
engine.save_to_file("Hello World!!!",'~/abc.mp3')

"""更改速率"""
rate = engine.getProperty('rate') # getting details of current speaking rate
print (rate) # printing current voice rate
engine.setProperty('rate', 125) # setting up new voice rate


"""更改音量"""
volume = engine.getProperty('volume') # getting to know current volume level (min=0 and max=1)
print (volume) # printing current volume level
engine.setProperty('volume',1.0) # setting up volume level between 0 and 1

"""更改声音"""
voices = engine.getProperty('voices') # getting details of current voice
#engine.setProperty('voice', voices[0].id) # changing index, changes voices. o for male
engine.setProperty('voice', voices[1].id) # changing index, changes voices. 1 for female

engine.say("Hello World!")
engine.say('My current speaking rate is ' + str(rate))
engine.runAndWait()
engine.stop()

官方文档

详细文档请看:官方文档