(python)文本转语音
pyttsx3(离线工作)是python中的文本到语音转换库
官方网址:
https://pyttsx3.readthedocs.io/en/latest/engine.html#module-pyttsx3.voice
安装:
pip install pyttsx3
说明文档:
创建对象
engine = pyttsx3.init()
获取当前语音速率
rate = engine.getProperty('rate')
print(f'语音速率:{rate}')
设置新的语音速率
engine.setProperty('rate', 200)
获取当前语音音量
volume = engine.getProperty('volume')
print(f'语音音量:{volume}')
设置新的语音音量,音量最小为 0,最大为 1
engine.setProperty('volume', 1.0)
获取当前语音声音的详细信息
voices = engine.getProperty('voices')
print(f'语音声音详细信息:{voices}')
设置当前语音声音为女性,当前声音不能读中文
engine.setProperty('voice', voices[1].id)
设置当前语音声音为男性,当前声音可以读中文
engine.setProperty('voice', voices[0].id)
获取当前语音声音
voice = engine.getProperty('voice')
print(f'语音声音:{voice}')
读txt文档
with open(r"文档地址.txt","r",encoding="utf-8") as f:
engine.say(f.line())
官方示例
1.口语文本
import pyttsx3
engine = pyttsx3.init()
engine.say('Sally sells seashells by the seashore.')
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()
2.将语音保存到文件
import pyttsx3
engine = pyttsx3.init()
engine.save_to_file('Hello World' , 'test.mp3')
engine.runAndWait()
3. 侦听事件
import pyttsx3
def onStart(name):
print 'starting', name
def onWord(name, location, length):
print 'word', name, location, length
def onEnd(name, completed):
print 'finishing', name, completed
engine = pyttsx3.init()
engine.connect('started-utterance', onStart)
engine.connect('started-word', onWord)
engine.connect('finished-utterance', onEnd)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()
4.中断话语
import pyttsx3
def onWord(name, location, length):
print 'word', name, location, length
if location > 10:
engine.stop()
engine = pyttsx3.init()
engine.connect('started-word', onWord)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()
5.改变声音
engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
engine.setProperty('voice', voice.id)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()
6.更改语速
engine = pyttsx3.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', rate+50)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()
7.更改音量
engine = pyttsx3.init()
volume = engine.getProperty('volume')
engine.setProperty('volume', volume-0.25)
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()
8.运行驱动程序事件循环
engine = pyttsx3.init()
def onStart(name):
print 'starting', name
def onWord(name, location, length):
print 'word', name, location, length
def onEnd(name, completed):
print 'finishing', name, completed
if name == 'fox':
engine.say('What a lazy dog!', 'dog')
elif name == 'dog':
engine.endLoop()
engine = pyttsx3.init()
engine.connect('started-utterance', onStart)
engine.connect('started-word', onWord)
engine.connect('finished-utterance', onEnd)
engine.say('The quick brown fox jumped over the lazy dog.', 'fox')
engine.startLoop()
9.使用外部事件循环
engine = pyttsx3.init()
engine.say('The quick brown fox jumped over the lazy dog.', 'fox')
engine.startLoop(False)
engine.iterate() must be called inside externalLoop()
externalLoop()
engine.endLoop()
无法切换到男声可参考:https://www.cnblogs.com/2944014083-zhiyu/p/14868455.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix