import pyttsx3
def text_to_speech(text):
# 初始化 TTS 引擎
engine = pyttsx3.init()
# 设置语音属性(可选)
# 比如设置语速(默认值是 200)
rate = engine.getProperty('rate')
engine.setProperty('rate', rate - 50) # 稍微慢一点的语速
# 比如设置音量(默认值是 1.0,范围是 0.0 到 1.0)
volume = engine.getProperty('volume')
engine.setProperty('volume', volume + 0.25) # 稍微大一点的音量
# 设置声音(可选,根据系统不同,可用的声音ID也不同)
# voices = engine.getProperty('voices')
# for voice in voices:
# print(f"ID: {voice.id}")
# engine.setProperty('voice', voices[0].id) # 设置为第一个声音
# 说出文本
engine.say(text)
# 等待所有语音指令完成
engine.runAndWait()
if __name__ == "__main__":
text = "你好,今天是星期五,谁是最帅的人"
text_to_speech(text)