我选择 🐶
import pyttsx3 as pyttsx
# 调用初始化方法,获取讲话对象
engine = pyttsx.init()
engine.say('2022年11月11日')
engine.runAndWait()
或
from win32com.client import Dispatch
# 获取讲话对象
speaker = Dispatch('SAPI.SpVoice')
# 讲话内容
speaker.Speak('你好!')
speaker.Speak('睡得还好吗?')
# 释放对象
del speaker
或
from comtypes.client import CreateObject
from comtypes.gen import SpeechLib
# 获取语音对象,源头
engine = CreateObject('SAPI.SpVoice')
# 输出到目标对象的流
stream = CreateObject('SAPI.SpFileStream')
infile = 'src/demo.txt'
outfile = 'src/demo_audio.wav'
# 获取流写入通道
stream.open(outfile, SpeechLib.SSFMCreateForWrite)
# 给语音源头添加输出流
engine.AudioOutputStream = stream
# 读取文本内容
# 打开文件
f = open(infile, 'r', encoding='utf-8')
# 读取文本内容
theText = f.read()
# 关闭流对象
f.close()
# 语音对象,读取文本内容
engine.speak(theText)
stream.close()