Text-to-speech (TTS) module that works under Python 3

A user on Reddit found a solution.

Turns out that gTTS works under Python 3.x, it was me that was importing the module wrong.

I was using:

import gtts
blabla = ("Spoken text")
tts = gTTS(text=blabla, lang='en')
tts.save("C:/test.mp3")

Resulting in the following error:

NameError: name 'gTTS' is not defined

When the correct way is:

from gtts import gTTS
blabla = ("Spoken text")
tts = gTTS(text=blabla, lang='en')
tts.save("C:/test.mp3")

The best solution for that is :

pyttsx3


Pyttsx3 is an offline cross-platform Text-to-Speech library which is compatible with both Python 3 and Python 2 and supports multiple TTS engines.

I have found it very useful and there is no delay in sound production unlike gTTS which needs internet connection to work and also has some delay.

To install :

Here is a sample code :


    import pyttsx3
    engine = pyttsx3.init()
    engine.say("Hello this is me talking")
    engine.setProperty('rate',120)  #120 words per minute
    engine.setProperty('volume',0.9) 
    engine.runAndWait()