本文主要是介绍Python调用pyttsx3实现离线文字转语音,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 femaleengine.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()
这篇关于Python调用pyttsx3实现离线文字转语音的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!