如何在python中将文本转换为语音

如何在python中将文本转换为语音,python,text-to-speech,speech,Python,Text To Speech,Speech,我现在想知道如何用python将文本转换为语音 在我使用的.NET中 Dim SAPI Msg = 'Hi this is a test' SAPI = CreateObject("sapi.spvoice") SAPI.Speak(Msg) 您可以通过模块来实现它。它使用默认的MS语音识别系统 import pyttsx engine = pyttsx.init() engine.say("Your Message") engine.runAndWait() 我知道在这里回答太晚了,

我现在想知道如何用python将文本转换为语音

在我使用的.NET中

Dim SAPI

Msg = 'Hi this is a test'

SAPI = CreateObject("sapi.spvoice")
SAPI.Speak(Msg)
您可以通过模块来实现它。它使用默认的MS语音识别系统

import pyttsx

engine = pyttsx.init()
engine.say("Your Message")
engine.runAndWait()

我知道在这里回答太晚了,但我想我会在这里发布,因为我有一个基于
TTS
转换的解决方案,使用
python
中的
SAPI
,这是OP最初的问题

这对于在
python
中使用
SAPI
寻找解决方案的任何其他人来说都很有用

from win32com.client import constants, Dispatch

Msg = "Hi this is a test"

speaker = Dispatch("SAPI.SpVoice")  #Create SAPI SpVoice Object
speaker.Speak(Msg)                  #Process TTS
del speaker                         #Delete speaker object and free up memory

您可以使用该模块来完成此操作。它将文本转换为语音。 您必须使用的第二个模块是播放转换后的文本

    from gtts import gTTS #pip install gtts
    import playsound #pip install playsound
    import os

    my_aud = gTTS("hello how are you") #converts the text into speech
    my_aud.save('demo.mp3') #save the file with .mp3 extension
    playsound('demo.mp3') #to play it
    os.remove('demo.mp3')

您能告诉我如何将其语音更改为女性语音(也是语言)我个人没有这样做,但根据研究,可以通过设置
Sapi.SpVoice.voice
属性来更改语音。请参阅此处的SAPI文档
import pyttsx3
speaker=pyttsx3.init()
speaker.say("Your message")
speaker.runAndWait()