Macintosh 中的 Python 文本到语音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12758591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:48:19  来源:igfitidea点击:

Python Text to Speech in Macintosh

pythonmacostext-to-speech

提问by VeilEclipse

Are there any libraries in Python that does or allows Text To Speech Conversion using Mac Lion's built in text to speech engine? I did google but most are windows based. I tried pyttx. I tried to run

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

But I get these errors

但我收到这些错误

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

How do I solve these errors?

我该如何解决这些错误?

采纳答案by rien333

Wouldn't it be much simpler to do this?

这样做不是更简单吗?

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

You can enter man sayto see other things you can do with the saycommand.

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

However, if you want some more advanced features, importing AppKitwould also be a possibility, although some Cocoa/Objective C knowledge is needed.

但是,如果您想要一些更高级的功能,导入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!')

If you would like to see more things you can do with NSSpeechSynthesizer take a look at Apple's documentation: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSSpeechSynthesizer_Class/Reference/Reference.html

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

回答by arainchi

If you are targeting Mac OS X as your platform - PyObjC and NSSpeechSynthesizer is your best bet.

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

Here is a quick example for you

这是一个快速示例

#!/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)

Please note that AppKit module is part of PyObjC bridge and should be already installed on your Mac. No need to install it if you are using OS provided python (/usr/bin/python)

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

回答by theguy

This might work:

这可能有效:

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