如何让 Python 说话
我怎样才能让 Python 说出一些文本?
我可以将 Festival 与子进程一起使用,但我无法控制它(或者可能在交互模式下,但它不会是干净的)。
有Python TTS 库吗?比如 Festival、eSpeak 的 API...?
How could I make Python say some text?
I could use Festival with subprocess but I won't be able to control it (or maybe in interactive mode, but it won't be clean).
Is there a Python TTS library? Like an API for Festival, eSpeak, ... ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您应该尝试使用 PyTTSx 包,因为 PyTTS 已过时。 PyTTSx 可与 Python 2 配合使用。对于 Python 3,请安装 PyTTSx3 包。
http://pypi.python.org/pypi/pyttsx/
https://pypi.org/project/pyttsx3/
You should try using the PyTTSx package since PyTTS is outdated. PyTTSx works with Python 2. For Python 3, install the PyTTSx3 package.
http://pypi.python.org/pypi/pyttsx/
https://pypi.org/project/pyttsx3/
有点俗气,但如果你使用 Mac,你可以从 python 将终端命令传递到控制台。
尝试在终端中输入以下内容:
Mac 上将会发出这样的声音。从 python 中,这样的事情相对容易:
A bit cheesy, but if you use a mac you can pass a terminal command to the console from python.
Try typing the following in the terminal:
And there will be a voice from the mac that will speak that. From python such a thing is relatively easy:
安装
pip install pypiwin32
如何使用 Windows PC 的文本转语音功能
使用 google 文本转语音 Api 创建 mp3 并收听它
在 cmd 中安装 gtts 模块后:
pip安装gtts
install
pip install pypiwin32
How to use the text to speech features of a Windows PC
Using google text-to-speech Api to create an mp3 and hear it
After you installed the gtts module in cmd:
pip install gtts
python-espeak 软件包可在 Debian、Ubuntu、Redhat 和其他 Linux 发行版中使用。它有最近的更新,并且工作正常。
Jonathan Leaders 指出,它也适用于 Windows,并且您也可以安装 mbrola 语音。请参阅 espeak 网站 http://espeak.sourceforge.net
The python-espeak package is available in Debian, Ubuntu, Redhat, and other Linux distributions. It has recent updates, and works fine.
Jonathan Leaders notes that it also works on Windows, and you can install the mbrola voices as well. See the espeak website at http://espeak.sourceforge.net
简单的 Google 一下,我就找到了 pyTTS,以及一些有关它的文档。然而,它看起来没有维护,而且是微软语音引擎特有的。
至少在 Mac OS X 上,您可以使用
subprocess
调用say
命令,这对于扰乱您的同事来说非常有趣,但对于您来说可能不太有用。需要。听起来 Festival 也有一些公共 API:
Festival 提供了一个基于 BSD 套接字的接口。这允许 Festival 作为服务器运行并允许客户端程序访问它。基本上,服务器为连接到它的每个客户端提供一个新的命令解释器。服务器为每个客户端分叉,但这比等待 Festival 进程从头开始要快得多。此外,服务器可以在更大的机器上运行,从而提供更快的综合速度。 链接
还有一个完整-特色 C++ API,您可以用它来制作 Python 模块(这很有趣!)。 Festival 还提供了一个精简的 C API(继续滚动该文档),您可以一次性使用
ctypes
。也许您已经发现了市场中的一个漏洞?
A simple Google led me to pyTTS, and a few documents about it. It looks unmaintained and specific to Microsoft's speech engine, however.
On at least Mac OS X, you can use
subprocess
to call out to thesay
command, which is quite fun for messing with your coworkers but might not be terribly useful for your needs.It sounds like Festival has a few public APIs, too:
Festival offers a BSD socket-based interface. This allows Festival to run as a server and allow client programs to access it. Basically the server offers a new command interpreter for each client that attaches to it. The server is forked for each client but this is much faster than having to wait for a Festival process to start from scratch. Also the server can run on a bigger machine, offering much faster synthesis. linky
There's also a full-featured C++ API, which you might be able to make a Python module out of (it's fun!). Festival also offers a pared-down C API -- keep scrolling in that document -- which you might be able to throw
ctypes
at for a one-off.Perhaps you've identified a hole in the market?
有很多方法可以让 Python 在 Python3 和 Python2 中使用,其中两个很好的方法是:
如果您使用的是 mac,您的计算机中将内置 os 模块。您可以使用以下方式导入 os 模块:
然后您可以使用 os 使用 os.system 命令运行终端命令:
在终端中,让计算机说话的方式是使用“say”命令,从而使计算机简单地说话使用:
如果你想用它来表达一个变量,你可以使用:
让Python说话的第二种方法是使用
你必须使用
或为Python3
安装它然后你可以使用以下代码来获取它说话:
我希望这有帮助! :)
There are a number of ways to make Python speak in both Python3 and Python2, two great methods are:
If you are on mac you will have the os module built into your computer. You can import the os module using:
You can then use os to run terminal commands using the os.system command:
In terminal, the way you make your computer speak is using the "say" command, thus to make the computer speak you simply use:
If you want to use this to speak a variable you can use:
The second way to get python to speak is to use
You will have to install this using
or for Python3
You can then use the following code to get it to speak:
I hope this helps! :)
PYTTSX3
什么:
Pyttsx3 是一个 python 模块,它是 pyttsx 的现代克隆,经过修改以与最新版本的 Python 3 配合使用!
原因:< /strong>
它是多平台、离线工作、适用于任何Python版本。
如何:
可以使用
pip install pyttsx3
安装,用法与pyttsx相同:PYTTSX3
What:
Pyttsx3 is a python module which is a modern clone of pyttsx, modified to work with the latest versions of Python 3!
Why:
It is multi-platform, works offline, and works with any python version.
How:
It can be installed with
pip install pyttsx3
and usage is the same as pyttsx:您可以使用 python 的 espeak 进行文本到语音转换器。
这是一个示例 python 代码
P.S :如果您的 Linux 系统上没有安装 espeak,那么您需要先安装它。
打开终端(使用 ctrl + alt + T)并输入
You can use espeak using python for text to speech converter.
Here is an example python code
P.S : if espeak isn't installed on your linux system then you need to install it first.
Open terminal(using ctrl + alt + T) and type
我更喜欢使用 Google Text To Speech 库,因为它的声音更自然。
有一个限制。 gTTS 只能将文本转换为语音并保存。因此,您必须找到另一个模块或函数来播放该文件。 (例如:playsound)
Playsound 是一个非常简单的模块,只有一个功能,那就是播放声音。
保存mp3文件后可以直接调用playsound.playsound()。
I prefer to use the Google Text To Speech library because it has a more natural voice.
There is one limitation. gTTS can only convert text to speech and save. So you will have to find another module or function to play that file. (Ex: playsound)
Playsound is a very simple module that has one function, which is to play sound.
You can call playsound.playsound() directly after saving the mp3 file.
可能没有任何“Python 特定的”内容,但 KDE 和 GNOME 桌面提供文本转语音作为其辅助功能支持的一部分,并且还提供 Python 库绑定。
可以使用 python 绑定来控制文本转语音的桌面库。
如果在 JVM 上使用 Python 的 Jython 实现,则可以使用 FreeTTS 系统。
最后,OSX和Windows 具有用于文本转语音的本机 API。可以通过 ctypes 或其他机制(例如 COM)从 python 使用这些。
There may not be anything 'Python specific', but the KDE and GNOME desktops offer text-to-speech as a part of their accessibility support, and also offer python library bindings.
It may be possible to use the python bindings to control the desktop libraries for text to speech.
If using the Jython implementation of Python on the JVM, the FreeTTS system may be usable.
Finally, OSX and Windows have native APIs for text to speech. It may be possible to use these from python via ctypes or other mechanisms such as COM.
如果您使用的是 python 3 和 windows 10,我发现最有效的解决方案来自 Giovanni Gianni。这对我来说是男声:
我还发现了这个 视频在 youtube 上,所以如果你真的愿意,你可以找你认识的人制作你自己的 DIY tts 声音。
If you are using python 3 and windows 10, the best solution that I found to be working is from Giovanni Gianni. This played for me in the male voice:
I also found this video on youtube so if you really want to, you can get someone you know and make your own DIY tts voice.
这就是您正在寻找的。适用于 Mac 的完整 TTS 解决方案。
您可以使用此独立服务器或作为 Web 应用程序的托管 Mac 服务器:
http://wolfpaulus.com /jounal/mac/ttsserver/
This is what you are looking for. A complete TTS solution for the Mac.
You can use this standalone or as a co-location Mac server for web apps:
http://wolfpaulus.com/jounal/mac/ttsserver/
结合以下来源,以下代码仅使用
platform
和os
模块即可在 Windows、Linux 和 macOS 上运行:注意:此方法不安全,可能会被恶意文本利用。
Combining the following sources, the following code works on Windows, Linux and macOS using just the
platform
andos
modules:Note: This method is not secure and could be exploited by malicious text.
只需在 python 中使用这个简单的代码即可。
仅适用于 Windows 操作系统。
我个人用这个。
Just use this simple code in python.
Works only for windows OS.
I personally use this.