python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python离线文字转语音

Python调用pyttsx3实现离线文字转语音的方式

作者:培根芝士

pyttsx3是 Python 中的文本到语音的离线转换库,本文给大家介绍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 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()

到此这篇关于Python调用pyttsx3实现离线文字转语音的文章就介绍到这了,更多相关Python离线文字转语音内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文