How to change the voice in pyttsx3?
Asked Answered
D

6

9

This code is working but I'm only able to switch between the voices which came preInstalled in Microsoft Windows. These voices are "Microsoft David Mobile" and "Microsoft Zira Mobile".

Later I installed "Microsoft Kalpana Mobile" and set it as the default Windows voice. But still I'm not able to switch to "Microsoft Kalpana Mobile". The code is-

import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id) #changing index changes voices but ony 0 and 1 are working here
engine.say('Hello World')
engine.runAndWait()

Only 0 and 1 are working as indices inside voices[].

I want the "Microsoft Kalpana Mobile" to speak. I'm working on this project for past 2 months. If this doesn't work, all my efforts will go in vein. Please Help:(

Thanks in advance.

Dee answered 1/7, 2017 at 6:43 Comment(0)
D
21

You can try this code:

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()

Then instead of the for loop, just pick up your preferred voice.id

Datary answered 5/7, 2017 at 16:36 Comment(0)
C
5

I have just noticed. To set the language⇓ This is just my default language setting is 'ja_JP'.

import pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
    print voice
    if voice.languages[0] == u'en_US':
        engine.setProperty('voice', voice.id)
        break

engine.say('Hello World')
engine.runAndWait()

or

voice.name == 'Alex'
Chorea answered 8/1, 2018 at 10:1 Comment(0)
D
2
voices = engine.getProperty('voices')

"voices" is a list of voices. So we need to identify index of voice which we want to set. starting with index = 0. of first voice

run a loop and you will see voices with their indexes and names

import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
index = 0
for voice in voices:
   print(f'index-> {index} -- {voice.name}')
   index +=1
engine.runAndWait()

and then just set index "voices[3].id" for fourth voice as example with index "3"

engine.setProperty('voice', voices[3].id)

and complete code might look something like this:

import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[3].id)
engine.say("hello I am the voice from your PC")
engine.runAndWait()
Distorted answered 19/1, 2021 at 1:39 Comment(0)
S
1

You have to add the voice "Microsoft Kalpana Mobile" in the window's registry file. Kindly check this link so that you are clear how to add voice in window's registry.

  1. click window key + r
  2. type regedit and press Enter
  3. Expand Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech_OneCore\Voices\Tokens\
  4. There you found your all voices. Export the "Microsoft Kalpana Mobile" into your specific voices folder.
  5. Open that exported file in notepad and replace the "Speech_OneCore" with "Speech" in whole file and save it again.
  6. Now double click on that file and merge it again. Now try to use the "Microsoft Kalpana Mobile" voice.
Schoolboy answered 12/12, 2021 at 10:1 Comment(0)
J
0
import pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[3].id)
engine.say("hello")
engine.runAndWait()

If you already have a code like:

brain = "hello"

You can try this:

import pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say(brain)
engine.runAndWait()
Jaw answered 23/1, 2021 at 13:17 Comment(0)
O
0

Registry Editor

Windows 11 comes installed with 3 audios in its Voices folder present inside its parent Speech folder.

You need to add "Microsoft Kalpana Mobile" in the window's registry file.

Few voices and its address location.

  1. DAVID(index = 0)
  2. HAZEL(index = 1)
  3. ZIRA(index = 2)

These voices are stored at: *HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices*

You can print these addresses in any editor using the following code:

import pyttsx3

engine = pyttsx3.init('sapi5')

print('DAVID: ' + voices[0].id)
print('HAZEL: ' + voices[1].id)
print('ZIRA: ' + voices[2].id)

You can also use the following method to know the name of the audio.

.name

Orthodontics answered 6/2, 2022 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.