Python‎ > ‎Python Voice‎ > ‎

Alternative Text to Speech

Some of the built in text to speech in Python e.g. pyttsx3 are broken in later versions of Python e.g 3.8.1, albeit some claims will be fixed in 3.8.2

Interim, use Google Translates - Google Text To Speech


GTTS

pip install gtts --trusted-host pypi.org --trusted-host files.pythonhosted.org

Calling this will only generate an MP3 file.  You need to call out to the OS to run an MP3 player on the file.


CMDMP3

The sources and executable files for these programs can be found at:


The Github repo for the sources is here:


To play an MP3 file with either, enter:

cmdmp3 mp3filename
or
cmdmp3win mp3filename


SSL issues...

To prevent SSL issues when invoking the subprocess call, then make sure the following python file is available in the directory or on the path

no_ssl_verify.py

import warnings
import contextlib
import requests
from urllib3.exceptions import InsecureRequestWarning
old_merge_environment_settings = requests.Session.merge_environment_settings
@contextlib.contextmanager
def no_ssl_verification():
    opened_adapters = set()
    def merge_environment_settings(self, url, proxies, stream, verify, cert):
        # Verification happens only once per connection so we need to close
        # all the opened adapters once we're done. Otherwise, the effects of
        # verify=False persist beyond the end of this context manager.
        opened_adapters.add(self.get_adapter(url))
        settings = old_merge_environment_settings(self, url, proxies, stream, verify, cert)
        settings['verify'] = False
        return settings
    requests.Session.merge_environment_settings = merge_environment_settings
    try:
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', InsecureRequestWarning)
            yield
    finally:
        requests.Session.merge_environment_settings = old_merge_environment_settings
        for adapter in opened_adapters:
            try:
                adapter.close()
            except:
                pass


I created a small python file to import into my other programs...

speak.py

import subprocess
from gtts import gTTS
from no_ssl_verify import no_ssl_verification

def say(words, l="en"):
    with no_ssl_verification():
    tts = gTTS(words, lang=l)
    tts.save("words.mp3")
    subprocess.call(["cmdmp3win.exe","words.mp3"])


In my main code I referenced this

test.py

from speak import say

say("hello, my name is maria")
say("Ni hao, Wǒ jiào Wong","zh-CN")
say("bonjour, comment ca va, je m'appelle jean-paul", "fr")
say("Bonjourno, mi chiamo mario", "it")
say("hola, me llamo juanita", "es")


Install pywin32

pip install pywin32 --trusted-host=pypi.org --trusted-host=files.pythonhosted.org

Microsoft speech engine

If you use Microsoft Windows 10, it has a speech engine included.
Install the module win32com, then you can use this code:

import win32com.client as wincl
speak = wincl.Dispatch("SAPI.SpVoice")
speak.Speak("Hello World")

This only uses the default voice for Windows 10.







Comments