簡體   English   中英

如何在 python 中將文本轉換為語音

[英]How to convert text to speech in python

我現在想知道如何在 python 中將文本轉換為語音。

在 .NET 我使用

Dim SAPI

Msg = 'Hi this is a test'

SAPI = CreateObject("sapi.spvoice")
SAPI.Speak(Msg)

您可以通過pyttsx模塊實現它。 它使用默認的 MS 語音識別系統。

import pyttsx

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

我知道在這里回答真的很晚,但我想我會在這里發布,因為我有基於在python使用SAPI TTS轉換的解決方案,這是 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

您可以使用gTTS模塊來完成。 它將文本轉換為語音。 您必須使用的第二個模塊是playsound來播放轉換后的文本。

    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')
import pyttsx3
speaker=pyttsx3.init()
speaker.say("Your message")
speaker.runAndWait()
# pip install pywin32
# pip install pyttsx3
 
import pyttsx3

pyttsx3.speak('Hello Woeld')

這是我自己創建的男聲和女聲功能。

只需定義一個文件名並保存即可。 現在您可以將其導入到另一個文件中並一次又一次地重復使用。 #pip 安裝 pyttsx3

import pyttsx3
def femaleVoice(text):
    print("Program : "+text)
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[-1].id)
    engine.say(text)
    engine.runAndWait()

def maleVoice(text):
    print("Program : "+text)
    pyttsx3.speak(text)

femaleVoice("There we go.")#Text
maleVoice("There we go.")

如果你想獲得大量的聲音。 我們有500多個。

這是一個片段

python
import apiaudio 
import os


apiaudio.api_key = os.environ['APIKEY']
first_track = apiaudio.Orchestrator.create_audio(scriptText="Hello World my first audio track", 
                                                 voice="Ryan",
                                                 soundTemplate="jakarta")
print(first_track)

您只需要一個免費的 api 密鑰。 看看http://www.api.audio

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM