Search code examples
pythonvoicepyttsx3

Python : pyttsx3


I want to changing voices and speech rate in pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')

engine.setProperty('voice', voices[1].id)
        #And
engine.setProperty('rate', 150)

Solution

  • This is the code I use for a Mac. If you have a Windows computer, I don't know if it will work. Ok, here is the code to change the rate:

    import pyttsx3    
    engine = pyttsx3.init()
    rate = engine.getProperty('rate')
    engine.setProperty('rate', put the rate you want)
    engine.setProperty('voice', 'com.apple.speech.synthesis.voice.Alex')
    engine.say("what you want to say goes here")
        
    engine.runAndWait()
    

    Here is the code to finding other voices. Put what you want it to say for every one of the voices. Then if you find one that works, copy it and put it into where it says .com.apple.speech.synthesis.voice.Alex on the code example above.

    import pyttsx3
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
    for voice in voices:
        print(voice, voice.id)
        engine.setProperty('voice', voice.id)
        engine.say("Hello World!")
        engine.runAndWait()
        engine.stop()
    

    Let me know if it works.