目录
效果有点生硬
支持:文字转语音库,支持英文,中文,可以调节语速、语调等。
快速入门
## 安装 pip install pyttsx3
import pyttsx3
engine=pyttsx3.init() # 初始化
engine.say('hello word')# 设置读取内容
engine.say('轻轻的我走了,正如我轻轻的来')
# with open('./ku.txt','r',encoding='utf-8')as rf:
# engine.say(rf.read())
engine.runAndWait() # 执行朗诵
调节语速
-是变慢,+是变快
## 安装 pip install pyttsx3
import pyttsx3
msg = '''大江东去,浪淘尽,千古风流人物。故垒西边,人道是:三国周郎赤壁。乱石穿空,惊涛拍岸,卷起千层雪。江山如画,一时多少豪杰。
遥想公瑾当年,小乔初嫁了,雄姿英发。羽扇纶巾,谈笑间,樯橹灰飞烟灭。故国神游,多情应笑我,早生华发。人生初梦,一尊还酹江月'''
teacher = pyttsx3.init()
rate = teacher.getProperty('rate')
teacher.setProperty('rate', rate - 10)
teacher.say(msg)
teacher.runAndWait()
注意只能变换英文,读取中文是不能变的;
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
print(voice, voice.id)
engine.setProperty('voice', voice.id)
engine.say("开心")
engine.runAndWait()
engine.stop()
生成文件mp3文件
# Import the required module
import pyttsx3
# Create a string
string = "Lorem Ipsum is simply dummy text " \
+ "of the printing and typesetting industry."
# Initialize the Pyttsx3 engine
engine = pyttsx3.init()
# We can use file extension as mp3 and wav, both will work
engine.save_to_file(string, 'speech.mp3')
# Wait until above command is not finished.
engine.runAndWait()
?