Python文本转语音库:pyttsx3 初识
大学计算机基础
共 1451字,需浏览 3分钟
·
2021-11-18 01:28
pyttsx3 初识
一、pyttsx3 概述
pyttsx3是Python中的文本到语音转换库。与其他库不同,它可以脱机工作,并且与Python 2和3兼容。
二、安装
安装命令
pip install pyttsx3
三、简单示例
#coding=utf-8
import pyttsx3
""" 语音播放 Hello World """
pyttsx3.speak("Hello World!")
或者
#coding=utf-8
import pyttsx3
""" 语音播放 Hello World """
engine = pyttsx3.init()
engine.say("Hello World!")
engine.runAndWait()
四、进阶示例
import pyttsx3
engine = pyttsx3.init() # object creation
""" 把语音存储到文件 """
engine.save_to_file("Hello World!!!",'~/abc.mp3')
"""更改速率"""
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()
视频教程(1)
视频教程(2)
最后视频答案,有会做的小伙伴可以后台留言噢!
我们一起学Python吧
评论