python语音读文本
使用 pyttsx3 语音库
读一个文本文件。。。
import pyttsx3
import os
rootdir=os.path.dirname( os.path.abspath(__file__) )+'/'
def sayy(filePath):
engine=pyttsx3.init()
print('开始播报')
#语速150
#rate=engine.getProperty('rate')
engine.setProperty('rate', 135)
#声音大小
volume=engine.getProperty('volume')
engine.setProperty('volume', 0.6)
#默认声音
voices=engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
file=open(filePath,'r',encoding='utf8')
text=''
lines=file.readlines()
for line in lines:
engine.say(line)
engine.runAndWait()
sayy(rootdir+"wenben1.txt")