自动化之文本朗读

pyttsx3

文本朗读功能主要利用了pyttsx3库,该库可以实现文字转语音功能

官方文档:https://pyttsx3.readthedocs.io/en/latest/engine.html

下面来看详细说明:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class ReadInner(object):
def __init__(self):
pass

def strReadInner(self, str):
engine = pyttsx3.init()
engine.say(str)
engine.runAndWait()

def fileReadInner(self, filename):
engine = pyttsx3.init()

# 设置发音速率,默认值为200
rate = engine.getProperty('rate')
engine.setProperty('rate', rate - 30)

# 设置发音大小,范围为0.0-1.0
volume = engine.getProperty('volume')
engine.setProperty('volume', 0.6)

# 需要自己下载语音包安装
# 0:汉语女声;1:英语男声;2:英语女声;3:日语女声;4:韩语女声;5:英语女声;6:粤语女声;7:台语女声
voices = engine.getProperty('voices')
voices = engine.setProperty('voice', voices[0].id)

if os.path.exists(filename):
with open(filename, 'r',encoding='utf-8') as file:
line = file.readline()
while line:
engine.say(line)
line = file.readline()
else:
with open(filename, 'w') as file:
print('Create a new file named %s' % filename)
engine.runAndWait()

# 检查已有的语音包
def checkExistedVoicePack(self):
engine = pyttsx3.init() # 初始化
voices = engine.getProperty('voices')
for voice in voices:
print('id = {} \nname = {} \n'.format(voice.id, voice.name))

注释已经很详细了~

微软小冰

https://azure.microsoft.com/en-us/services/cognitive-services/text-to-speech/#overview

暂不知如何调用,待更

不要打赏,只求关注呀QAQ