Custom Def #1: Import pyttsx3

Published May 27, 2020, noon

In this tutorial series we will learn to make and use custom definitions in our python programs.  I made this program to create fill-in-the-blank silly stories for my kids.  Part of the fun of this program is the text-to-speech engine that can be used by python 3 called pyttsx3. In this first session we will just make sure that the module that we need is installed and get it to work.

In your text editor, create a new file and start with some import statements:

import sys, os
import pyttsx3

If you do not have the pyttsx3 module then you will need to install it.  I check to see if modules are installed by running a python interactive session and passing the import _module_name_ command.  If the module is installed, python will not give you any errors.  If it is not installed python will tell you that it can't find it.

If you don't have it, that is no problem.  Open the python terminal and type the following command:

pip install pyttsx3

Pip is a python package installer that comes with python 3. 

Once the package is installed, we can proceed.  If you are trying to install packages with the anaconda version of python you would try to install them using:

conda install _package_name_

I tried to see if this would work with the anaconda version of python.  It didn't.  That is why I have the vanilla version of python 3 on my machine as well.

Anyway, after we install those modules, we will start our file with a few definitions.

def onStart(name):
	print(name)
	
def onWord(name, location, length):
	print("word", name, location, length)
	
def onEnd(name, completed):
	print('Finishing', name, completed)
	

Don't worry about what these definitions are for just yet.  Each one will be an input that we need to use the text-to-speech engine.

Next we will initialize our TTS engine as follows:

engine = pyttsx3.init()
engine.connect('started-utterance', onStart)

In our code we are going to have the program print the text to the screen and then say it. The pyttsx3 engine 'connect' attribute looks for keywords.  From the help file:

     |      started-utterance: name=<str>
     |      started-word: name=<str>, location=<int>, length=<int>
     |      finished-utterance: name=<str>, completed=<bool>
     |      error: name=<str>, exception=<exception>

If we wanted a slightly different behavior, we could have used another keyword and the other definitions that we made.  Again, if this doen't make sense to you then just move on for now. 

Next we will supply some text, feed that text to the engine, and then have the engine print and say the text out loud.

text = "I am a robot.  Beep, boop."
engine.say(text, text)
engine.runAndWait()

Save that as 'madlib.py' and then at the command terminal run the program:

python madlib.py

The computer should have spoken the words that we supplied as text.  At this point you should feel free to play around with this  module.  In the next tutorial, I will show you how to get different voices and how to get the speech engine to read a short story.  See you next time!

skip_nextCustom Def #2: Read a story from lines.
  • Custom Def #1: Import pyttsx3
    (currently viewing)
  • Custom Def #2: Read a story from lines.

  • Custom Def #3: Using Multiple Definitions

  • Custom Def #4: Generalized Definitions

  • Custom Def #5: Dictionaries and Definitions

  • Custom Def #6: Python Pickles

  • Custom Def #7: Search and Replace in Lines

  • Custom Def #8: Now for the Story...