如何让 Python 说话

发布于 2024-08-09 06:53:59 字数 141 浏览 10 评论 0原文

我怎样才能让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

做个ˇ局外人 2024-08-16 06:53:59

您应该尝试使用 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/

猥琐帝 2024-08-16 06:53:59

有点俗气,但如果你使用 Mac,你可以从 python 将终端命令传递到控制台。

尝试在终端中输入以下内容:

$ say 'hello world' 

Mac 上将会发出这样的声音。从 python 中,这样的事情相对容易:

import os
os.system("echo 'hello world'")
os.system("say 'hello world'") 

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:

$ say 'hello world' 

And there will be a voice from the mac that will speak that. From python such a thing is relatively easy:

import os
os.system("echo 'hello world'")
os.system("say 'hello world'") 
我早已燃尽 2024-08-16 06:53:59

安装
pip install pypiwin32

如何使用 Windows PC 的文本转语音功能

from win32com.client import Dispatch

speak = Dispatch("SAPI.SpVoice").Speak

speak("Ciao")

使用 google 文本转语音 Api 创建 mp3 并收听它

在 cmd 中安装 gtts 模块后:
pip安装gtts

from gtts import gTTS
import os    

tts = gTTS(text="This is the pc speaking", lang='en')
tts.save("pcvoice.mp3")
# to start the file from python
os.system("start pcvoice.mp3")

install
pip install pypiwin32

How to use the text to speech features of a Windows PC

from win32com.client import Dispatch

speak = Dispatch("SAPI.SpVoice").Speak

speak("Ciao")

Using google text-to-speech Api to create an mp3 and hear it

After you installed the gtts module in cmd:
pip install gtts

from gtts import gTTS
import os    

tts = gTTS(text="This is the pc speaking", lang='en')
tts.save("pcvoice.mp3")
# to start the file from python
os.system("start pcvoice.mp3")
沦落红尘 2024-08-16 06:53:59

python-espeak 软件包可在 Debian、Ubuntu、Redhat 和其他 Linux 发行版中使用。它有最近的更新,并且工作正常。

from espeak import espeak
espeak.synth("Hello world.")

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.

from espeak import espeak
espeak.synth("Hello world.")

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

眉黛浅 2024-08-16 06:53:59

简单的 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 the say 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?

云淡风轻 2024-08-16 06:53:59

有很多方法可以让 Python 在 Python3 和 Python2 中使用,其中两个很好的方法是:

  • 使用 os

如果您使用的是 mac,您的计算机中将内置 os 模块。您可以使用以下方式导入 os 模块:

import os

然后您可以使用 os 使用 os.system 命令运行终端命令:

os.system("Your terminal")

在终端中,让计算机说话的方式是使用“say”命令,从而使计算机简单地说话使用:

os.system("say 'some text'")

如果你想用它来表达一个变量,你可以使用:

os.system("say " + myVariable)

让Python说话的第二种方法是使用

  • pyttsx 模块

你必须使用

pip install pyttsx3

或为Python3

pip3 install pyttsx3

安装它然后你可以使用以下代码来获取它说话:

import pyttsx3
engine = pyttsx3.init()

engine.say("Your Text")

engine.runAndWait()

我希望这有帮助! :)

There are a number of ways to make Python speak in both Python3 and Python2, two great methods are:

  • Using os

If you are on mac you will have the os module built into your computer. You can import the os module using:

import os

You can then use os to run terminal commands using the os.system command:

os.system("Your terminal")

In terminal, the way you make your computer speak is using the "say" command, thus to make the computer speak you simply use:

os.system("say 'some text'")

If you want to use this to speak a variable you can use:

os.system("say " + myVariable)

The second way to get python to speak is to use

  • The pyttsx module

You will have to install this using

pip install pyttsx3

or for Python3

pip3 install pyttsx3

You can then use the following code to get it to speak:

import pyttsx3
engine = pyttsx3.init()

engine.say("Your Text")

engine.runAndWait()

I hope this helps! :)

对你的占有欲 2024-08-16 06:53:59

PYTTSX3

什么:

Pyttsx3 是一个 python 模块,它是 pyttsx 的现代克隆,经过修改以与最新版本的 Python 3 配合使用!

原因:< /strong>

它是多平台离线工作适用于任何Python版本

如何:

可以使用pip install pyttsx3安装,用法与pyttsx相同:

import pyttsx3;
engine = pyttsx3.init();
engine.say("I will speak this text");
engine.runAndWait();

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:

import pyttsx3;
engine = pyttsx3.init();
engine.say("I will speak this text");
engine.runAndWait();
木落 2024-08-16 06:53:59

您可以使用 python 的 espeak 进行文本到语音转换器。
这是一个示例 python 代码

    from subprocess import call
    speech="Hello World!"
    call(["espeak",speech])

P.S :如果您的 Linux 系统上没有安装 espeak,那么您需要先安装它。
打开终端(使用 ctrl + alt + T)并输入

    sudo apt install espeak

You can use espeak using python for text to speech converter.
Here is an example python code

    from subprocess import call
    speech="Hello World!"
    call(["espeak",speech])

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

    sudo apt install espeak
梅倚清风 2024-08-16 06:53:59

我更喜欢使用 Google Text To Speech 库,因为它的声音更自然。

from gtts import gTTS
def speak(text):
  tts = gTTS(text=text, lang="en")
  filename = "voice.mp3"
  tts.save(filename)

有一个限制。 gTTS 只能将文本转换为语音并保存。因此,您必须找到另一个模块或函数来播放该文件。 (例如:playsound)

Playsound 是一个非常简单的模块,只有一个功能,那就是播放声音。

import playsound
def play(filename):
  playsound.playsound(filename)

保存mp3文件后可以直接调用playsound.playsound()。

I prefer to use the Google Text To Speech library because it has a more natural voice.

from gtts import gTTS
def speak(text):
  tts = gTTS(text=text, lang="en")
  filename = "voice.mp3"
  tts.save(filename)

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.

import playsound
def play(filename):
  playsound.playsound(filename)

You can call playsound.playsound() directly after saving the mp3 file.

用心笑 2024-08-16 06:53:59

可能没有任何“Python 特定的”内容,但 KDE 和 GNOME 桌面提供文本转语音作为其辅助功能支持的一部分,并且还提供 Python 库绑定。
可以使用 python 绑定来控制文本转语音的桌面库。

如果在 JVM 上使用 Python 的 Jython 实现,则可以使用 FreeTTS 系统。

最后,OSXWindows 具有用于文本转语音的本机 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.

染墨丶若流云 2024-08-16 06:53:59

如果您使用的是 python 3 和 windows 10,我发现最有效的解决方案来自 Giovanni Gianni。这对我来说是男声:

import win32com.client as wincl
speak = wincl.Dispatch("SAPI.SpVoice")
speak.Speak("This is the pc voice speaking")

我还发现了这个 视频在 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:

import win32com.client as wincl
speak = wincl.Dispatch("SAPI.SpVoice")
speak.Speak("This is the pc voice speaking")

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.

无戏配角 2024-08-16 06:53:59

这就是您正在寻找的。适用于 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/

孤寂小茶 2024-08-16 06:53:59

结合以下来源,以下代码仅使用 platformos 模块即可在 Windows、Linux 和 macOS 上运行:

tx = input("Text to say >>> ")
tx = repr(tx)

import os
import platform

syst = platform.system()
if syst == 'Linux' and platform.linux_distribution()[0] == "Ubuntu":
    os.system('spd-say %s' % tx)
elif syst == 'Windows':
    os.system('PowerShell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak(%s);"' % tx)
elif syst == 'Darwin':
    os.system('say %s' % tx)
else:
    raise RuntimeError("Operating System '%s' is not supported" % syst)

注意:此方法不安全,可能会被恶意文本利用。

Combining the following sources, the following code works on Windows, Linux and macOS using just the platform and os modules:

tx = input("Text to say >>> ")
tx = repr(tx)

import os
import platform

syst = platform.system()
if syst == 'Linux' and platform.linux_distribution()[0] == "Ubuntu":
    os.system('spd-say %s' % tx)
elif syst == 'Windows':
    os.system('PowerShell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak(%s);"' % tx)
elif syst == 'Darwin':
    os.system('say %s' % tx)
else:
    raise RuntimeError("Operating System '%s' is not supported" % syst)

Note: This method is not secure and could be exploited by malicious text.

残龙傲雪 2024-08-16 06:53:59

只需在 python 中使用这个简单的代码即可。

仅适用于 Windows 操作系统。

from win32com.client import Dispatch

def speak(text):
    speak = Dispatch("SAPI.Spvoice")
    speak.Speak(text)

speak("How are you dugres?")

我个人用这个。

Just use this simple code in python.

Works only for windows OS.

from win32com.client import Dispatch

def speak(text):
    speak = Dispatch("SAPI.Spvoice")
    speak.Speak(text)

speak("How are you dugres?")

I personally use this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文