Simple Text to Speech Demo in Python 3 (with pyttsx3)

Text to Speech pyttsx3


How to run the demo?
Download and unzip GeeXLab and the demopack where you want, launch GeeXLab and drop the demo (main.xml) in GeeXLab. This demo requires OpenGL 2.1 and Python 3.8+.

pyttsx3 is a text to speech conversion library for Python 3. This library does not require an internet connection, that’s good, GeeXLab loves offline libs!

pyttsx3 is shipped with GeeXLab 0.47.2 in the python3_8/Lib/site-packages/ folder. If you want to play with pyttsx3 in no time, just grab the latest GeeXLab + the demopack. Now, if you want to install pyttsx3, open a terminal and type:

pip install pyttsx3

Let’s see how to use pyttsx3 to hear more or less any speech from a string. Here is a simple example that import the pyttsx3 library, initializes it and play a text:

import pyttsx3 as t2s
t2s_engine = t2s.init()
tex = "The quick brown fox jumped over the lazy dog."

pyttsx3 offers the possibility to change the rate (number of words per minute), the volume, the voice and even the language:

rate = t2s_engine.getProperty('rate')
t2s_engine.setProperty('rate', rate)

volume = t2s_engine.getProperty('volume')
t2s_engine.setProperty('volume ', volume )

voices = t2s_engine.getProperty('voices')    
t2s_engine.setProperty('voice', voices[0].id)

I prepared a GeeXLab demo that allows you to type a short text and change the rate:

GeeXLab demo: Text to Speech with pyttsx3

This demo is available in the Python 3 demopack in the geexlab-demopack-python3/speech/01-text-2-speech/ folder.

Remark: the runAndWait() function blocks the demo. If the text is short that should be ok but for longer texts that can be a problem. One solution would be to run the text to speech conversion in a separate thread using a ZOMBIE script and shared variables or shared memory.

Leave a Comment

Your email address will not be published. Required fields are marked *