【问题标题】:Saving Text to Speech Python将文本保存到语音 Python
【发布时间】:2018-09-27 00:23:13
【问题描述】:

我正在尝试将文本到语音的结果保存到 Windows 上的文件中。我成功地让它说话(使用speak.Speak)。但是,保存文件就没有这样的运气了。

问题是没有找到AudioOutputStream,尽管它是being listed in the Microsoft docs

版本信息:Windows 10、Python 3.6

错误

Traceback (most recent call last):
  File "...\dd.py", line 87, in <module>
    speak.AudioOutputStream = filestream
  File "...\win32com\client\dynamic.py", line 565, in __setattr__
    self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
pywintypes.com_error: (-2147352573, 'Member not found.', None, None)

代码

from win32com.client import Dispatch
import win32api

speak = Dispatch("SAPI.SpVoice")
filestream = Dispatch("SAPI.SpFileStream")
filestream.open("out.wav", 3, False) 
for k in speak.GetAudioOutputs():
    print(k.GetDescription())
speak.AudioOutputStream = filestream
speak.Speak("test")
filestream.close()

【问题讨论】:

    标签: python text-to-speech


    【解决方案1】:

    我设法让它工作。由于最初对这篇文章的回应很少,我相信将此修复留给未来的访问者会非常有用。

    使用pip install comtypes 安装库。与 python 的原生 comtype 实现相比,它更加协作且不那么笨拙。

    import comtypes.client
    
    speak = comtypes.client.CreateObject("SAPI.SpVoice")
    filestream = comtypes.client.CreateObject("SAPI.spFileStream")
    filestream.open("out.wav", 3, False)
    speak.AudioOutputStream = filestream 
    speak.Speak("test")
    filestream.close()
    

    【讨论】:

      【解决方案2】:

      我发现了这个问题,但 Neil 使用 comtypes 提供的解决方案对我不起作用,并给了我一个错误。

      但是,他最初的解决方案确实适用于我的系统:

      • Windows 10 专业版,版本 19.0.19041 内部版本 19041
      • Python 3.8.1 64 位

      我用它来为语音时钟创建 .wav 文件

      import win32com.client
      
      phrases = [
          "Zero",
          "One",
          "Two",
          "Three",
          "Four",
          "Five",
          "Six",
          "Seven",
          "Eight",
          "Nine",
          "Ten",
          "Eleven",
          "Twelve",
          "Thirteen",
          "Fourteen",
          "Fifteen",
          "Sixteen",
          "Seventeen", 
          "Eighteen",
          "Nineteen",
          "Twenty",
          "Thirty",
          "Fourty",
          "Fifty",
          "Hundred",
      ]
      
      speaker = win32com.client.Dispatch("SAPI.SpVoice")
      filestream = win32com.client.Dispatch("SAPI.SpFileStream")
      
      def speak(phrase):
          speaker.speak(phrase)
          
      def save_voice(phrase):
          filestream.open(".".join([phrase, "wav"]), 3, False)
          speaker.AudioOutputStream = filestream
          speaker.speak(phrase)
          filestream.close()
      
      for phrase in phrases:
          save_voice(phrase)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-07
        • 2019-11-23
        • 1970-01-01
        • 2017-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多