【问题标题】:How to convert text to speech in python如何在python中将文本转换为语音
【发布时间】:2012-11-25 10:38:59
【问题描述】:

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

在 .NET 中我使用过

Dim SAPI

Msg = 'Hi this is a test'

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

【问题讨论】:

    标签: python text-to-speech speech


    【解决方案1】:

    您可以通过pyttsx 模块来实现。它使用默认的 MS 语音识别系统。

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

    【讨论】:

      【解决方案2】:

      我知道在这里回答真的晚了,但我想我会在这里发布,因为我有基于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
      

      【讨论】:

      • 你能告诉我怎样才能把它的声音改成女声(也是语言)
      • 我没有亲自做过,但是根据研究,可能可以通过设置Sapi.SpVoice.Voice属性来改变声音。请参阅此处的 SAPI 文档microsoft.com/en-us/download/details.aspx?id=10121
      【解决方案3】:

      您可以使用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')
      

      【讨论】:

        【解决方案4】:
        import pyttsx3
        speaker=pyttsx3.init()
        speaker.say("Your message")
        speaker.runAndWait()
        

        【讨论】:

          【解决方案5】:
          # pip install pywin32
          # pip install pyttsx3
           
          import pyttsx3
          
          pyttsx3.speak('Hello Woeld')
          

          【讨论】:

            【解决方案6】:

            这是我自己创建的男声和女声功能。
            只需定义一个文件名并保存即可。
            现在您可以将其导入另一个文件并一次又一次地重复使用。

            pip install 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.")
            

            【讨论】:

              猜你喜欢
              • 2013-03-09
              • 2010-10-04
              • 2011-05-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多