在现代应用程序中,文本转语音(Text-to-Speech, TTS)技术越来越受到重视。无论是为视力障碍人士提供帮助,还是为教育和娱乐应用增添趣味,TTS 都能发挥重要作用。今天,我们将介绍一个简单易用的 Python 库——pyttsx3,它可以帮助你轻松实现文本转语音功能。
什么是 pyttsx3?
pyttsx3 是一个 Python 库,用于将文本转换为语音。与其他 TTS 库不同,pyttsx3 是一个离线库,这意味着它不依赖于互联网连接,可以在本地计算机上运行。官网:https://github.com/nateshmbhat/pyttsx3
它支持多种语音引擎,包括 SAPI5(Windows)、NSSpeechSynthesizer(macOS)和 espeak(Linux)。具体不通系统使用的语音引擎为:
Linux | macOS | Windows | |
---|---|---|---|
AVSpeech | ✅︎ | ||
eSpeak | ✅︎ | ✅︎ | ✅︎ |
NSSpeechSynthesizer | ✅︎ | ||
SAPI5 | ✅︎ |
实践操作
安装pyttsx3
只要安装pyttsx3库即可。如果是在linux系统,需要安装espeak-ng库。(windows下以前已经安装过espeak-ng库,所以倒不确定了)
pip install pyttsx3
sudo apt update && sudo apt install espeak-ng libespeak1
使用
简单使用
初始化引擎,然后朗读文本
import pyttsx3
engine = pyttsx3.init()
# For Mac, If you face error related to "pyobjc" when running the `init()` method :
# Install 9.0.1 version of pyobjc : "pip install pyobjc>=9.0.1"
engine.say("I will speak this text")
engine.runAndWait()
最简单语句使用
只需要一条命令就可以直接朗读文本
import pyttsx3
pyttsx3.speak("I will speak this text")
修改语音voice、速率rate和音量volume
import pyttsx3
engine = pyttsx3.init() # object creation
# RATE
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
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
# VOICE
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()
# Saving Voice to a file
# On Linux, make sure that 'espeak-ng' is installed
engine.save_to_file('Hello World', 'test.mp3')
engine.runAndWait()
支持说中文语音
import pyttsx3
def say_chinese(text):
engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
if "Chinese" in voice.id:
engine.setProperty('voice', voice.id)
break
engine.say(text)
engine.runAndWait()
say_chinese("你好,世界")
试了一下,效果相当不错!
或者说,是目前测试的最好的!