python3之语音合成pyttsx3库使用

pyttsx3

pyttsx3是一个Python库,‌用于将文本转换为语音输出。‌它支持多种操作系统,‌包括Windows和macOS,‌并且提供了丰富的功能来控制文本到语音的转换过程。‌

import pyttsx3

msg = "Hello World!"
pyttsx3.speak(msg)

engine = pyttsx3.init()
engine.say(msg)
engine.runAndWait()

""" 把语音存储到文件 """
engine.save_to_file(msg, "~/abc.mp3")


"""更改速率"""
rate = engine.getProperty("rate")
print(rate)
engine.setProperty("rate", 125)


"""更改音量"""
volume = engine.getProperty("volume")
print(volume)
engine.setProperty("volume", 1.0)  # 0-1


"""语音"""
voice = engine.getProperty("voice")
print(voice)
voices = engine.getProperty("voices")
for voice in voices:
    print(voice)
engine.setProperty("voice", voices[1].id)

engine.say(msg)
engine.runAndWait()
engine.stop()

 

posted @ 2024-08-02 10:09  carol2014  阅读(6)  评论(0编辑  收藏  举报