Change pyttsx3 language

Student picture Student · Jan 31, 2021 · Viewed 9.1k times · Source

When trying to use pyttsx3 I can only use English voices. I would like to be able to use Dutch as well.

I have already installed the text to speech language package in the windows settings menu. But I can still only use the deafaut english voice.

How can I fix this?

Answer

sergej picture sergej · Feb 21, 2021

If you want to change a language you need to change to another "voice" that supports your language.

  1. To see which voices/languages are installed you can list them like this:
import pyttsx3

engine = pyttsx3.init()

for voice in engine.getProperty('voices'):
    print(voice)
  1. No you can change to your favorite voice like this:
engine.setProperty('voice', voice.id)

I personally use this helper function I mentioned here also

# language  : en_US, de_DE, ...
# gender    : VoiceGenderFemale, VoiceGenderMale
def change_voice(engine, language, gender='VoiceGenderFemale'):
    for voice in engine.getProperty('voices'):
        if language in voice.languages and gender == voice.gender:
            engine.setProperty('voice', voice.id)
            return True

    raise RuntimeError("Language '{}' for gender '{}' not found".format(language, gender))

And finally, you can use it like this (if language and gender are installed):

import pyttsx3

engine = pyttsx3.init()
change_voice(engine, "nl_BE", "VoiceGenderFemale")
engine.say("Hello World")
engine.runAndWait()