文本转语音的Python库(pyttsx3)

一、pyttsx3的概述

pyttsx3 is a text-to-speech conversion library in Python.
pyttsx3是Python中的文本到语音转换库。

二、pyttsx3的安装

pip install pyttsx3

三、小试牛刀

import pyttsx3
engine = pyttsx3.init()
engine.say("I will speak this text right now")
engine.runAndWait()
"""速率"""
rate = engine.getProperty('rate')  # 获取当前发音速率
print(f'语速:{rate}')  # 默认200
engine.setProperty('rate', 125)  # 设置新的发音速率
engine.say("I will speak this text right now")
engine.runAndWait()  
"""音量"""
volume = engine.getProperty('volume')   # 获取当前音量值(min=0 and max=1)
print(volume)
engine.setProperty('volume', 0.5)    # 设置音量值(between 0 and 1)
engine.say("I will speak this text right now")
engine.runAndWait()
"""音色"""
voices = engine.getProperty('voices')
for voice in voices:
    print(voice)
engine.setProperty('voice', voices[0].id)
engine.say("I will speak this text right now")
engine.runAndWait()
"""保存语音文件"""
engine.save_to_file("I will speak this text right now", 'test.mp3')
engine.runAndWait()

你可能感兴趣的:(python库,python)