简体   繁体   English

Python中的Python文本到语音

[英]Python Text to Speech in Macintosh

Are there any libraries in Python that does or allows Text To Speech Conversion using Mac Lion's built in text to speech engine? Python中是否有任何库使用Mac Lion的内置文本到语音引擎来实现或允许文本到语音转换? I did google but most are windows based. 我做谷歌,但大多数是基于Windows的。 I tried pyttx. 我试过pyttx。 I tried to run 我试着跑

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? 我该如何解决这些错误?

Wouldn't it be much simpler to do this? 这样做不是更简单吗?

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

You can enter man say to see other things you can do with the say command. 您可以输入man say以查看使用say命令可以执行的其他操作。

However, if you want some more advanced features, importing AppKit would also be a possibility, although some Cocoa/Objective C knowledge is needed. 但是,如果您想要一些更高级的功能,虽然需要一些Cocoa / Objective C知识,但也可以导入AppKit

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的

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. 请注意,AppKit模块是PyObjC桥的一部分,应该已经安装在Mac上。 No need to install it if you are using OS provided python (/usr/bin/python) 如果您使用的是OS提供的python(/ usr / bin / python),则无需安装它

This might work: 这可能有效:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM