import pyttsx3
def read_poem_with_voice(filename):
"""
使用女声从文本文件中读取古诗词并朗读
:param file_path: 文本文件的路径
"""
engine = pyttsx3.init() # 初始化语音引擎
# 设置语速(范围通常是50-200)
engine.setProperty('rate', 115)
# 尝试设置语音为女性(注意:这取决于你的系统安装的语音包)
voices = engine.getProperty('voices')
for voice in voices:
if voice.gender == 'female':
engine.setProperty('voice', voice.id)
break
with open(filename, 'r', encoding='utf-8') as file:
poem = file.read().strip() # 读取整首诗并去除首尾空白字符
# 如果诗很长,可能需要分段朗读
# 这里我们假设整首诗可以直接朗读
engine.say(poem)
engine.runAndWait() # 开始朗读并等待结束
# 使用函数朗读古诗词
read_poem_with_voice('text')
07-10
1330
1330

被折叠的 条评论
为什么被折叠?



