python – How to register voice for TTS in windows 11?


I have some troubles to use 2 additional voices, in my case i try to use Pablo (Spanish – Spain) and George (English – GB). I develop a app in python/Django, from the front i sent a JSON with the language (in this case with what voice i want to read the text), and the text, the back received and process it calling the proper voice reading the text. here is part of the code:

import win32com.client, pythoncom
from .translator import translate_text


def check_voices():

    pythoncom.CoInitialize()

    try:
        speaker = win32com.client.Dispatch('SAPI.SpVoice')
        voices = speaker.GetVoices()

        for voice in voices:
            print(f"Voice: {voice.GetDescription()}, Id: {voice.Id}")
    finally:
        # Finalizar COM
        pythoncom.CoUninitialize()


def sound_rate(speaker):
    speaker.Rate = -1  # rango -10(lento) - 10(rapido)


def sound_volume(speaker):
    speaker.Volume = 100    # rango 0(bajo) - 100(alto)


def text_to_speech(language, text):

    pythoncom.CoInitialize()

    try:
        speaker = win32com.client.Dispatch('SAPI.SpVoice')
        voices = speaker.GetVoices()

        check_voices()

        if language.startswith('en-'):
            text = translate_text(text, from_code="es", to_code="en")        

        language_map = {
            'es-ES-female': 'Microsoft Helena Desktop - Spanish (Spain)',
            'es-ES-male': 'Microsoft Pablo Desktop - Spanish (Spain)',
            'en-UK-female': 'Microsoft Hazel Desktop - English (Great Britain)',
            'en-UK-male': 'Microsoft George Desktop - English (Great Britain)',
        }


        voice_name = language_map.get(language)
        for voice in voices:
            if voice.GetDescription() == voice_name:
                speaker.Voice = voice
                break

        speaker.Speak(text)

    except Exception as e:
        print(f"An error occurred: {e}")

    finally:
       pythoncom.CoUninitialize()

for some reason, i can’t use this two voices Pablo and George, i try to install both of them with powershell in admin mode, but still not working. Also i try to write directly in my windows registry, but also didn’t work.

enter image description here
enter image description here
enter image description here
enter image description here

i try to find the path to the voices and i found it in C:\Windows\Speech_OneCore\Engines\TTS\es-ES both of them. but i copy paste this files also in C:\Windows\Speech\Engines\TTS\es-ES

when i run my app and i try to use this two voices i see this error:

[19/Jul/2024 09:11:22] "GET / HTTP/1.1" 200 1580
[19/Jul/2024 09:11:23,062] - Broken pipe from ('127.0.0.1', 55799)
Not Found: /favicon.ico
[19/Jul/2024 09:11:23,099] - Broken pipe from ('127.0.0.1', 55804)
<QueryDict: {}>
Voice: Microsoft Helena Desktop - Spanish (Spain), Id: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ES-ES_HELENA_11.0
Voice: Microsoft Zira Desktop - English (United States), Id: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0
Voice: Microsoft George Desktop - English (Great Britain), Id: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN_GB_GEORGE_11.0
Voice: Microsoft Hazel Desktop - English (Great Britain), Id: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-GB_HAZEL_11.0
Voice: Microsoft Pablo Desktop - Spanish (Spain), Id: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ES-ES_PABLO_11.0
[19/Jul/2024 09:11:31] "POST /submit/ HTTP/1.1" 200 93
An error occurred: (-2147352567, 'Ocurrió una excepción.', (0, None, 'Clase no registrada\r\n', None, 0, -2147221164), None)
<QueryDict: {}>
Voice: Microsoft Helena Desktop - Spanish (Spain), Id: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ES-ES_HELENA_11.0
Voice: Microsoft Zira Desktop - English (United States), Id: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0
Voice: Microsoft George Desktop - English (Great Britain), Id: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN_GB_GEORGE_11.0
Voice: Microsoft Hazel Desktop - English (Great Britain), Id: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-GB_HAZEL_11.0
Voice: Microsoft Pablo Desktop - Spanish (Spain), Id: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ES-ES_PABLO_11.0
An error occurred: (-2147352567, 'Ocurrió una excepción.', (0, None, 'Clase no registrada\r\n', None, 0, -2147221164), None)
[19/Jul/2024 09:11:39] "POST /submit/ HTTP/1.1" 200 93

can someone help me to solve it?. Thanks

Note: I use Django == 5.0.6 and Python == 3.10.6



Source link

Leave a Comment