【问题标题】:Python Text to Speech in MacintoshMacintosh 中的 Python 文本转语音
【发布时间】:2012-09-27 08:07:02
【问题描述】:

Python 中是否有任何库可以使用 Mac Lion 的内置文本到语音引擎执行或允许文本到语音转换? 我做了谷歌,但大多数都是基于 Windows 的。我试过pyttx。 我试着跑了

import pyttsx
engine = pyttsx.init()
engine.say('Sally sells seashells by the seashore.')
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

但我得到了这些错误

File "/Users/manabchetia/Documents/Codes/Speech.py", line 2, in <module>
    engine = pyttsx.init()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyttsx-1.0.egg/pyttsx/__init__.py", line 39, in init
    eng = Engine(driverName, debug)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyttsx-1.0.egg/pyttsx/engine.py", line 45, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyttsx-1.0.egg/pyttsx/driver.py", line 64, in __init__
    self._module = __import__(name, globals(), locals(), [driverName])
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyttsx-1.0.egg/pyttsx/drivers/nsss.py", line 18, in <module>
ImportError: No module named Foundation

如何解决这些错误?

【问题讨论】:

  • 当然你可以使用 OSX say 命令?
  • @MartijnPieters:我用谷歌搜索并尝试了 pyttx,但出现错误。
  • @ManabChetia:这是一个实质性的区别,现在你有一个实际的问题! :-)
  • Foundationpyobjc library 的一部分;你安装了吗?它默认带有 system python,但我看到你安装了自己的 Python 2.7。
  • @MartijnPieters:我还没有安装它。所以我现在需要输入easy_install pyobjc

标签: python macos text-to-speech


【解决方案1】:

这样做不是更简单吗?

from os import system
system('say Hello world!')

您可以输入man say 以查看您可以使用say 命令执行的其他操作。

但是,如果您想要一些更高级的功能,也可以导入AppKit,尽管需要一些 Cocoa/Objective C 知识。

from AppKit import NSSpeechSynthesizer
speechSynthesizer = NSSpeechSynthesizer.alloc().initWithVoice_("com.apple.speech.synthesis.voice.Bruce")
speechSynthesizer.startSpeakingString_('Hi! Nice to meet you!')

如果您想了解更多可以使用 NSSpeechSynthesizer 执行的操作,请查看 Apple 的文档:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSSpeechSynthesizer_Class/Reference/Reference.html

【讨论】:

  • 如果 hello world 在变量中,我如何让say 说话?我试过 AppKit 但它不可用。 `easy_install' 也无法做到这一点。我该怎么做?
  • 如果你想将一个变量传递给 say 命令,你可以这样做: str = "Test" system('say %s' % (str))
  • 此答案中的代码 sn-p 略有偏差。 .alloc是一个函数,所以应该是.alloc()
  • 在字符串中包含撇号可能会导致这不起作用。例如,尝试执行 system("Can't touch this") 将运行而不会出现错误,但实际上不会大声说出任何内容。
  • @ProQ 正常的 shell 引用规则当然适用。例如,您可以将该字符串嵌入文字引号中(就像您通常必须这样做的那样)
【解决方案2】:

如果您将 Mac OS X 作为您的平台 - PyObjC 和 NSSpeechSynthesizer 是您的最佳选择。

这是一个简单的例子

#!/usr/bin/env python

from  AppKit import NSSpeechSynthesizer
import time
import sys


if len(sys.argv) < 2:
   text = raw_input('type text to speak> ')
else:
   text = sys.argv[1]

nssp = NSSpeechSynthesizer

ve = nssp.alloc().init()

for voice in nssp.availableVoices():
   ve.setVoice_(voice)
   print voice
   ve.startSpeakingString_(text)

   while not ve.isSpeaking():
      time.sleep(0.1)

   while ve.isSpeaking():
      time.sleep(0.1)

请注意,AppKit 模块是 PyObjC 桥的一部分,应该已经安装在您的 Mac 上。如果您使用的是操作系统提供的 python (/usr/bin/python),则无需安装它

【讨论】:

【解决方案3】:

这可能有效:

import subprocess
subprocess.call(["say","Hello World! (MESSAGE)"])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    • 2016-09-07
    • 1970-01-01
    相关资源
    最近更新 更多