这可能是迟到的答案,但我希望它会有用:)
# pip install comtypes
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
voiceList = []
for voice in voices:
voiceList.append(voice.name)
print("Voice List: " ,voiceList)
def playItNow(textf, filename, useFile = True, rate = 2, voice = voiceList[0]):
from comtypes.client import CreateObject
engine = CreateObject("SAPI.SpVoice")
engine.rate = rate # can be -10 to 10
for v in engine.GetVoices():
if v.GetDescription().find(voice) >= 0:
engine.Voice = v
break
else:
print("Voice not found")
if useFile:
datei = open(textf, 'r',encoding="utf8")
text = datei.read()
datei.close()
else:
text = textf
stream = CreateObject("SAPI.SpFileStream")
from comtypes.gen import SpeechLib
stream.Open(filename, SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
engine.speak(text)
stream.Close()
import winsound
winsound.PlaySound(filename, winsound.SND_FILENAME)
playItNow("TextFile.txt", "test_2.wav", useFile= False, rate = -1)