import sys
from win32com.client import constants
import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
while 1:
try:
s = input('put your world here!')
if s == "exit speaker":
break
speaker.Speak(s)
except:
if sys.exc_type is EOFError:
sys.exit()
也可以发中文,需要装中文包:
微软语音开发包 SpeechSDK51.exe
微软语音开发-语言包 SpeechSDK51LangPack.exe
例子:
import win32com.client
import os
content = '中国!'
sp = win32com.client.Dispatch('SAPI.SpVoice')
voice_list = sp.GetVoices()
voices = dict([(os.path.basename(voice.Id), voice) for voice in voice_list])
sp.Rate = -5
sp.Voice = voices['MSSimplifiedChineseVoice']
sp.Speak(content)