How to make Python speak

前端 未结 13 1188
时光说笑
时光说笑 2020-11-30 20:34

How could I make Python say some text?

I could use Festival with subprocess but I won\'t be able to control it (or maybe in interactive mode, but it won\'t be clean)

相关标签:
13条回答
  • 2020-11-30 20:37

    Please note that this only work with python 2.x

    You should try using the PyTTSx package since PyTTS is outdated. PyTTSx works with the lastest python version.

    http://pypi.python.org/pypi/pyttsx/1.0 -> The package

    Hope it helps

    0 讨论(0)
  • 2020-11-30 20:39

    If you are using python 3 and windows 10, the best solution that I found to be working is from Giovanni Gianni. This played for me in the male voice:

    import win32com.client as wincl
    speak = wincl.Dispatch("SAPI.SpVoice")
    speak.Speak("This is the pc voice speaking")
    

    I also found this video on youtube so if you really want to, you can get someone you know and make your own DIY tts voice.

    0 讨论(0)
  • 2020-11-30 20:42

    Combining the following sources, the following code works on Windows, Linux and macOS using just the platform and os modules:

    • cantdutchthis' answer for the mac command
    • natka_m's comment for the Ubuntu command
    • BananaAcid's answer for the Windows command
    • Louis Brandy's answer for how to detect the OS
    • nc3b's answer for how to detect the Linux distribution
    tx = input("Text to say >>> ")
    tx = repr(tx)
    
    import os
    import platform
    
    syst = platform.system()
    if syst == 'Linux' and platform.linux_distribution()[0] == "Ubuntu":
        os.system('spd-say %s' % tx)
    elif syst == 'Windows':
        os.system('PowerShell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak(%s);"' % tx)
    elif syst == 'Darwin':
        os.system('say %s' % tx)
    else:
        raise RuntimeError("Operating System '%s' is not supported" % syst)
    

    Note: This method is not secure and could be exploited by malicious text.

    0 讨论(0)
  • 2020-11-30 20:48

    install pip install pypiwin32

    How to use the text to speech features of a Windows PC

    from win32com.client import Dispatch
    
    speak = Dispatch("SAPI.SpVoice")
    
    speak.Speak("Ciao")
    

    Using google text-to-speech Api to create an mp3 and hear it

    After you installed the gtts module in cmd: pip install gtts

    from gtts import gTTS
    import os    
    
    tts = gTTS(text="This is the pc speaking", lang='en')
    tts.save("pcvoice.mp3")
    # to start the file from python
    os.system("start pcvoice.mp3")
    
    0 讨论(0)
  • 2020-11-30 20:48

    There may not be anything 'Python specific', but the KDE and GNOME desktops offer text-to-speech as a part of their accessibility support, and also offer python library bindings. It may be possible to use the python bindings to control the desktop libraries for text to speech.

    If using the Jython implementation of Python on the JVM, the FreeTTS system may be usable.

    Finally, OSX and Windows have native APIs for text to speech. It may be possible to use these from python via ctypes or other mechanisms such as COM.

    0 讨论(0)
  • 2020-11-30 20:50

    A bit cheesy, but if you use a mac you can pass a terminal command to the console from python.

    Try typing the following in the terminal:

    $ say 'hello world' 
    

    And there will be a voice from the mac that will speak that. From python such a thing is relatively easy:

    import os
    os.system("echo 'hello world'")
    os.system("say 'hello world'") 
    
    0 讨论(0)
提交回复
热议问题