将文本保存为语音 Python

问题描述 投票:0回答:3

我正在尝试将文本转语音的结果保存到 Windows 上的文件中。我成功让它说话了(使用

speak.Speak
)。然而,保存文件就没那么幸运了。

问题是

AudioOutputStream
未被找到,尽管它已在 Microsoft 文档中列出

版本信息: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
3个回答
4
投票

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

使用

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()

0
投票

我发现了这个问题,但 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)

-2
投票

查看 Pyt2s 库。它有如此多的声音和语言。只需要连接互联网即可使用。

安装库:

pip install pyt2s

示例代码:

from pyt2s.services import stream_elements

obj = stream_elements.StreamElements() 

data = obj.requestTTS('Lorem Ipsum is simply dummy text.')

with open('output.mp3', '+wb') as file:
    file.write(data)
© www.soinside.com 2019 - 2024. All rights reserved.