pyttsx3
是 Python 中一个功能强大且易于使用的文本转语音(Text - to - Speech, TTS)库,具有以下特点:
跨平台支持
- 可在 Windows、Linux 和 macOS 等多个主流操作系统上使用。这使得开发者无论使用何种操作系统,都能方便地利用该库实现文本转语音功能,为不同平台的应用开发提供了便利。
离线工作
- 无需网络连接即可工作。相比一些依赖在线 API 的文本转语音服务,
pyttsx3
在本地运行,这不仅保证了数据的隐私和安全性,还避免了因网络不稳定或无网络环境导致的功能受限问题,适用于多种场景,如本地自动化脚本、单机应用程序等。
官网平台
https://pypi.org/project/pyttsx3/
安装命令Installation
pip install pyttsx3
Collecting pyttsx3
Downloading pyttsx3-2.98-py3-none-any.whl.metadata (3.8 kB)
Collecting comtypes (from pyttsx3)
Downloading comtypes-1.4.10-py3-none-any.whl.metadata (7.2 kB)
Collecting pypiwin32 (from pyttsx3)
Downloading pypiwin32-223-py3-none-any.whl.metadata (236 bytes)
Collecting pywin32 (from pyttsx3)
Downloading pywin32-310-cp311-cp311-win_amd64.whl.metadata (9.4 kB)
Downloading pyttsx3-2.98-py3-none-any.whl (34 kB)
Downloading comtypes-1.4.10-py3-none-any.whl (241 kB)
---------------------------------------- 241.5/241.5 kB 12.5 kB/s eta 0:00:00
Downloading pypiwin32-223-py3-none-any.whl (1.7 kB)
Downloading pywin32-310-cp311-cp311-win_amd64.whl (9.5 MB)
---------------------------------------- 9.5/9.5 MB 18.0 kB/s eta 0:00:00
Installing collected packages: pywin32, pypiwin32, comtypes, pyttsx3
Successfully installed comtypes-1.4.10 pypiwin32-223 pyttsx3-2.98 pywin32-310
英文调用
import pyttsx3
engine = pyttsx3.init()
engine.say("I will speak this text")
中文调用
import pyttsx3
engine = pyttsx3.init()
#engine.say("I will speak this text")
engine.setProperty('voice', 'zh') #开启支持中文
rate = engine.getProperty('rate') #获取当前语速
engine.setProperty('rate', 'rate-40')
engine.setProperty('volume', '0.7')
engine.say("未来之窗,经过100年发展")
engine.runAndWait()
语音转文件wav
import pyttsx3
from datetime import datetime
engine = pyttsx3.init()
#engine.say("I will speak this text")
engine.setProperty('voice', 'zh') #开启支持中文
rate = engine.getProperty('rate') #获取当前语速
engine.setProperty('rate', 'rate-40')
engine.setProperty('volume', '0.7')
#engine.say("未来之窗,经过100年发展")
当前时间time = datetime.now()
当前时间str = 当前时间time.strftime("%Y_%m_%d_%H_%M_%S")
output_path = f"./wlzc_outwav/{当前时间str}.wav"
text = "未来之窗,经过100年发展"
engine.save_to_file(text, output_path)
engine.runAndWait()
engine.save_to_file(text, output_path)
嵌入数字人函数
def 仙盟_数字生命_文本转语音_pyttsx3(text_input, text_cache, voice_speed, voice_id):
# 模拟大模型的回答
print("================仙盟_数字生命_文本转语音_pyttsx3\n===============tacotron2-DDC")
print(f"================仙盟_数字生命_文本转语音_pyttsx3\n{text_cache}\完结")
TEXT = text_cache #text_cache
#output_path = 'es_output.wav'
当前时间time = datetime.now()
当前时间str = 当前时间time.strftime("%Y_%m_%d_%H_%M_%S")
output_path = f"./wlzc_outwav/{当前时间str}.wav"
仙盟_tts_pyttsx3 = pyttsx3.init()
仙盟_tts_pyttsx3.setProperty('voice', 'zh') #开启支持中文
仙盟_tts_pyttsx3 = 仙盟_tts_pyttsx3.getProperty('rate') #获取当前语速
仙盟_tts_pyttsx3.setProperty('rate', 'rate-40')
仙盟_tts_pyttsx3.setProperty('volume', '0.7')
仙盟_tts_pyttsx3.save_to_file(TEXT, output_path)
仙盟_tts_pyttsx3.runAndWait()
# 读取一个语音文件模拟语音合成的结果
#output_path = f"./wlzc_outwav/2025_04_13_00_26_16.wav"
with open(output_path, "rb") as audio_file:
audio_value = audio_file.read()
base64_string = base64.b64encode(audio_value).decode('utf-8')
return base64_string
运行数字人