import pyttsx3
def use_pyttsx3():
# 創建對象
engine = pyttsx3.init()
# 獲取當前語音速率
rate = engine.getProperty('rate')
print(f'語音速率:{rate}')
# 設置新的語音速率
engine.setProperty('rate', 200)
# 獲取當前語音音量
volume = engine.getProperty('volume')
print(f'語音音量:{volume}')
# 設置新的語音音量,音量最小為 0,最大為 1
engine.setProperty('volume', 1.0)
# 獲取當前語音聲音的詳細信息
voices = engine.getProperty('voices')
print(f'語音聲音詳細信息:{voices}')
# 設置當前語音聲音為女性,當前聲音不能讀中文
engine.setProperty('voice', voices[1].id)
# 設置當前語音聲音為男性,當前聲音可以讀中文
engine.setProperty('voice', voices[0].id)
# 獲取當前語音聲音
voice = engine.getProperty('voice')
print(f'語音聲音:{voice}')
# 語音文本
path = 'test.txt'
with open(path, encoding='utf-8') as f_name:
words = str(f_name.readlines()).replace(r'\n', '')
# 將語音文本說出來
engine.say(words)
engine.runAndWait()
engine.stop()
if __name__ == '__main__':
use_pyttsx3()