在 Python 3 下运行的文本转语音 (TTS) 模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30612298/
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-19 08:42:12  来源:igfitidea点击:

Text-to-speech (TTS) module that works under Python 3

pythonpython-3.xtext-to-speech

提问by Macondo

I have tried PyTTS (deprecated) and PyTTSx (the most recommended) and two Google TTS solutions (gTTS and another one by some guy named Hung Truong) but none of them worked under Python 3.4. It seems they haven't been ported to 3.x.

我已经尝试过 PyTTS(已弃用)和 PyTTSx(最推荐的)和两个 Google TTS 解决方案(gTTS 和另一个名为 Hung Truong 的人的解决方案),但它们都没有在 Python 3.4 下工作。似乎它们还没有被移植到 3.x。

I searched here on StackOverflow and Google, but all the proposed TTS solutions don't work under Python 3. I'm on Windows 7.

我在 StackOverflow 和 Google 上搜索过,但所有提议的 TTS 解决方案都不能在 Python 3 下运行。我在 Windows 7 上。

采纳答案by Macondo

A user on Reddit found a solution.

Reddit 上的一位用户找到了解决方案

Turns out that gTTS works under Python 3.x, it was me that was importing the module wrong.

原来 gTTS 在 Python 3.x 下工作,是我错误地导入了模块。

I was using:

我正在使用:

import gtts
blabla = ("Spoken text")
tts = gTTS(text=blabla, lang='en')
tts.save("C:/test.mp3")

Resulting in the following error:

导致以下错误:

NameError: name 'gTTS' is not defined

When the correct way is:

当正确的方法是:

from gtts import gTTS
blabla = ("Spoken text")
tts = gTTS(text=blabla, lang='en')
tts.save("C:/test.mp3")

回答by sgp

Rather than using a module, you could use Google Text-to-Speech API . You can easily use this URL to generate a wav file and get it through a simple HTTP request:

您可以使用 Google Text-to-Speech API,而不是使用模块。你可以很容易地使用这个 URL 来生成一个 wav 文件并通过一个简单的 HTTP 请求来获取它:

http://www.translate.google.com/translate_tts?tl=en&q=Hello%20World

http://www.translate.google.com/translate_tts?tl=en&q=Hello%20World

回答by Alex

I just installed gtts 1.0.7 that was uploaded on 2015-10-07

我刚刚安装了 2015-10-07 上传的 gtts 1.0.7

The following code works for me in Python 3.5:

以下代码适用于 Python 3.5:

import subprocess
from gtts import gTTS

audio_file = "hello.mp3"
tts = gTTS(text="Hello World!", lang="en")
tts.save(audio_file)
return_code = subprocess.call(["afplay", audio_file])

I'm on a Mac using in the inbuilt "afply" to play the mp3 but there are other ways e.g. Playing mp3 song on python

我在 Mac 上使用内置的“afply”来播放 mp3,但还有其他方式,例如在 python 上播放 mp3 歌曲

回答by Natesh bhat

The best solution for that is :

最好的解决方案是:

pyttsx3

pyttsx3



Pyttsx3 is an offline cross-platform Test-to-Speechlibrary which is compatible with both Python 3 and Python 2and supports multiple TTS engines.

Pyttsx3 是一个离线跨平台 Test-to-Speech库,兼容Python 3 和 Python 2,并支持多个 TTS 引擎。

I have found it very useful and there is no delay in sound production unlike gTTS which needs internet connectionto work and also has some delay.

我发现它非常有用,并且与gTTS不同,gTTS 需要互联网连接才能工作并且也有一些延迟,因此声音制作没有延迟。

To install :

安装 :

pip install pyttsx3

pip install pyttsx3

Here is a sample code :

这是一个示例代码:

```

``

import pyttsx3
engine = pyttsx3.init()
engine.say("Hello this is me talking")
engine.setProperty('rate',120)  #120 words per minute
engine.setProperty('volume',0.9) 
engine.runAndWait()

```

``