Python 3.4 - Text to Speech with SAPI

Livio picture Livio · Jul 1, 2015 · Viewed 12.2k times · Source

I was trying to use this code to convert text to speech with Python 3.4, but since my computer's main language is not English (I'm using Win7x64) the voice and the accent are wrong (Because I want it to "speak" English).

import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak("Hello, it works!")

so, is there a way to change the voice/language (of the program, not the system)? Also, do you think there is a better way to do this? Perhaps a module that can work on every sytem?

Answer

Lesley Gushurst picture Lesley Gushurst · Jul 1, 2015

Chances are that your OS only came with one voice as it is. There are several ways you can get English sounding output using IPA (International Phonetic Language) and SVSFIsXML as a flag in your speak call... but I'm guessing you'd want something less complicated than that.

The first thing I'd do is grab an English voice if you don't have one already. (Check first by going into your control panel->speech recognition-> text to speech and look at your voice selection. If it says "Microsoft Anna - English (United States)" then, yes you already have an English voice.)

If not you'll have to grab another voice Microsoft Speech Platform - Runtime Languages (Version 11) . I highly recommend Microsoft Server Speech Text to Speech Voice (en-US, ZiraPro) as an English voice. You'll also want Microsoft Speech Platform - Software Development Kit (SDK) (Version 11).

Honestly, I just kind of install them all because I think it's cool.

Once you've got those all installed, what I've found to get the voices working is a neat registry hack I found at Voice Attack - Getting free alternate TTS voices working with Win7/8 64bit.

Basically what this entails is that you do some string replacement in your MS Speech Platform voices in your registry so that what you see in your

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech Server\v11.0\Voices HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Speech Server\v11.0\Voices

registries will wind up in:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Speech\Voices

Once that's done, go back to control panel and look at all the voices you installed. You should be able to test them all, even in different languages. If the voices aren't playing then the voices you installed weren't the right bit (x86 vs 64).

Now in python you'll have to make a SetVoice call. I've never in my life programmed in python, but I imagine the call you'd want would look something like speaker.SetVoice("Microsoft Server Speech Text to Speech Voice (en-US, ZiraPro)"). After you set the voice, that voice should be the one speaking all the time when you make a Speak call.

Now if you have gotten to this point and the voices played in the control panel but not in your code, it could be that your program is 32bit/64bit or something, and then you gotta run back, reinstall the opposite 32bit/64bit voices, run your reg edits again, and try running your application again.

A bit of work, but it'll pay off. If you do distribute your code, you'll have to make sure your voices are part of the client's registry, and messing with that can be a headache in itself.