pyttsx3是 Python 中的文本到语音的离线转换库。
安装依赖:
pip install pyttsx3
Linux 安装要求:
apt update && apt install espeak ffmpeg libespeak1
使用方式:
import pyttsx3
engine = pyttsx3.init()
engine.say("I will speak this text")
engine.runAndWait()
带默认选项的朗读功能的单行用法
import pyttsx3
pyttsx3.speak("I will speak this text")
改变声音、语速和音量:
import pyttsx3
engine = pyttsx3.init() # object creation
# 语速
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()
# 保存到文件
# On linux make sure that 'espeak' and 'ffmpeg' are installed
engine.save_to_file('Hello World', 'test.mp3')
engine.runAndWait()