首页 > 解决方案 > 如何在 Python 中打开扬声器以说出给定的文本

问题描述

我怎样才能让 Python 说出一些文字?

我听说有一个 Python TTS 库。如何将其包含在程序中?以及我应该用给定的文本调用哪个函数来通过 CPU 扬声器说话。

标签: python-3.x

解决方案


您将需要该模块:gTTS (Google Text-to-Speech)

首先,安装模块 gTTS

pip install gTTS

然后创建一个python文件:

from gtts import gTTS

#Write your text
text_to_speech = "So that's how you convert text to speech using Python"

#Choose your language (en : english, fr: french, ...)
language = "en"

#Passing the text, language and the speed to the module
myobj = gTTS(text=text_to_speech, lang=language, slow=False)

#Saving the audio in mp3
myobj.save("text_to_speech.mp3")




推荐阅读