简体   繁体   中英

Pyttsx, I can't change the gender in the code…, what i'm doing wrong?

first awesome lib! I'm using under linux and was preety easy to start, but now I want to change the speech to female... I'm trying with the code below without success... can you please help me?

import pyttsx

def say(text):
    print "{}".format(text)
    engine = pyttsx.init()
    engine.setProperty('rate', 100)
    voices = engine.getProperty('voices')
    for voice in voices:
        print voice.gender
        engine.setProperty('female', voice.gender)
        print "change: {}".format(voice.gender)
    engine.say(text)
    engine.runAndWait()
engine.setProperty('female', voice.gender)

The engine doesn't have a property called female , so setting this property to the gender of the current voice doesn't make any sense and, as you have found, doesn't do anything.

I assume that you're intending to loop through all the available voices and choose the first one you find that's female. If so, maybe something like this:

for voice in voices:
    if voice.gender == "female"
        engine.setProperty('voice', voice.id)
        break

You could try the code below. It worked perfect for me:

for voice in voices:
     engine.setProperty('voice',voices[1].id)
      engine.say("Hello") 
 engine.runAndWait()

For me, the only thing that worked in Linux to change the voice to female was:

voices = speaker.getProperty('voices')
speaker.setProperty('voice', 'english+f4')
speaker.say('Finally, something worked!')
speaker.runAndWait()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM