python实现朗读内容

项目描述

pyttsx3 是 Python 中的文本到语音转换库。与替代库不同,它可以脱机工作,并且与Python 2和3兼容。

pyttsx3 的官网pyttsx3 ·皮皮 (pypi.org)https://pypi.org/project/pyttsx3/#files

安装

pip install pyttsx3

推荐使用国内源下载:

pip install pyttsx3 -i https://pypi.tuna.tsinghua.edu.cn/simple

如果收到诸如“无名为 win32com.client 的模块”、“无名为 win32 的模块”或“无名为 win32api 的模块”等错误,则需要另外安装 pypiwin32。

如何在安装出现错误请参考:

 Python的pyttsx3安装失败的解决方案_阿龙的代码在报错的博客-CSDN博客https://blog.csdn.net/yujinlong2002/article/details/126249398?spm=1001.2014.3001.5502用法:

#引入模块
import pyttsx3
# 初始化语音引擎
teacher = pyttsx3.init()
#设定朗读内容
msg = 'hello Word'
#合成并播放语音
teacher.say(msg)
# 等待语音播放完
teacher.runAndWait()

更改语音、速率和音量:

import pyttsx3
engine = pyttsx3.init() #对象创建

""" RATE"""
rate = engine.getProperty('rate')   # # 获取当前语速的详细信息
print (rate)                        #打印当前语音速率
engine.setProperty('rate', 125)     # 设置新的语音速率


"""VOLUME"""
volume = engine.getProperty('volume')   #了解当前音量水平(最小值 = 0 和最大值 = 1)
print (volume)                          #打印当前卷级别
engine.setProperty('volume',1.0)    # 将音量级别设置为 0 和 1 之间

"""VOICE"""
voices = engine.getProperty('voices')       #获取当前语音的详细信息
#engine.setProperty('voice', voices[0].id)  #改变索引,改变声音。o 为男性
engine.setProperty('voice', voices[1].id)   #改变索引,改变声音。1 为女性

engine.say("Hello World!")
engine.say('My current speaking rate is ' + str(rate))
engine.runAndWait()
engine.stop()

"""将语音保存到文件"""
# 在 linux 上,确保安装了 “espeak” 和 'ffmpeg'
engine.save_to_file('Hello World', 'test.mp3')
engine.runAndWait()

本人笔记希望对你有帮助

 

 

 

 

 

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿龙的代码在报错

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值