文本转换为语音

使用pyttsx

pip install pyttsx3

有时候需要管理员权限 ,添加--user

pip install pyttsx3 --user

  1. import pyttsx3 as pyttsx
  2. engine=pyttsx.init()
  3. engine.say('你好啊')
  4. engine.runAndWait()

使用SAPI

  1. from win32com.client import Dispatch
  2. speaker=Dispatch('SAPI.SpVoice')
  3. speaker.Speak('大家好')
  4. del speaker

使用SpeechLib

实现文本转换语音

安装
pip install comtypes

  1. from comtypes.client import CreateObject
  2. from comtypes.gen import SpeechLib
  3. egine=CreateObject('SAPI.SpVoice')
  4. stream=CreateObject('SAPI.SpFileStream')
  5. infile='demo.txt'
  6. outfile='demo.wav'
  7. stream.open(outfile,SpeechLib.SSFMCreateForWrite)
  8. egine.AudioOutputStream=stream
  9. f=open(infile,'r',encoding='utf-8')
  10. theText=f.read()
  11. f.close()
  12. egine.speak(theText)
  13. stream.close()

使用PocketSphinx

安装
pip install PocketSphinx

pip install SpeechRecognition

  1. import speech_recognition as sr
  2. audio_file='demo.wav'
  3. r=sr.Recognizer()
  4. with sr.AudioFile(audio_file) as source:
  5. audio=r.record(source)
  6. print('文本内容',r.recognize_sphinx(audio))

image.png