Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change pyttsx3 language

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?

like image 660
Cerabbite Avatar asked Mar 08 '26 20:03

Cerabbite


2 Answers

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()
like image 70
sergej Avatar answered Mar 11 '26 09:03

sergej


The language property "zh" denotes Mandarin Chinese. You can loop through all the voices included in pyttsx3 and at the time of this writing there are 3 supported Chinese voices (on a MacBook): Taiwanese, Hong Kong, and Chinese (mainland).

engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
   engine.setProperty('voice', voice.id)
   if "zh" in voice.id:
       print(voice.id)

I'm using a Mac, so the results may be different for you. But here is my result:

com.apple.voice.compact.zh-TW.Meijia
com.apple.voice.compact.zh-HK.Sinji
com.apple.voice.compact.zh-CN.Tingting

Here is an example code to audibly speak Chinese:

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', "com.apple.voice.compact.zh-CN.Tingting")
engine.say('炒菜的时候,也可以放')
engine.runAndWait()

If you want a Chinese voice, for example, on a Windows computer, it is not included by default. You will have to first manually add one. Then you can run through the first code sample above and find the voice with "zh" in it. I did a test on a Windows machine and the name of the voice.id was "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ZH-CN_HUIHUI_11.0"

like image 35
nhershy Avatar answered Mar 11 '26 11:03

nhershy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!