Custom Def #2: Read a story from lines.

Published May 27, 2020, 3:30 p.m.

One of the attributes of the pyttsx3 engine is that it has two voices; a generic male and female voice.  To start off this tutorial, we will play around with that feature.

Create some space after we've connected to our TTS engine.  Then add the lines:

voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)

This should change the voice of the TTS engine from a male voice to a female voice.  Now lets add a feature that randomly selects one voice or the other.

At the top of the file add the line:

import random

Then change the line where we set the property from voices[1].id to:

engine.setProperty('voice', random.choice(voices).id)

Now if you run your program a couple of times it should sometimes be the male voice and sometimes be the female voice.

Now we want to have the program read a whole story.  To start we will add the story directly to our program.  Later we will put the story into a seperate file and then read that file to get the story.  For now just copy and paste the following into your code:

lines = []
lines.append( "\n Now for the story! \n")
lines.append( "Once upon a time there was a dog with 2 weak arms.")
lines.append( "The dog was very small and frail.")
lines.append( "One day a mean dog-catcher spoke to the dog and said...")
lines.append( "'You are too weak.  You must eat protien shakes!'")
lines.append( "The dog was annoyed, but he started excercising daily.")
lines.append( "Pretty soon, he became stronger and more muscular!")
lines.append( "Now the dog shows off his muscles on his own workout videos sponsored by P-89-X!")
lines.append( "The End!")

Now we have a list of lines with a story to feed into the engine.  But to make this like a madlib we need to replace some of the words in the story.  Let's create a definition in the space above our story:

def getNoun():
	query = "Tell me a noun, and hit 'Enter'."
	engine.say(query,query)
	engine.runAndWait()
	word = input()
	return word

This definition is going to use a function called 'input' that lets the user add input as the progam is executed.  When used this definition will print to the screen and ask the user for a noun.  Lets get 4 nouns to use in our story:

Noun1 = getNoun()
Noun2 = getNoun()
Noun3 = getNoun()
Noun4 = getNoun()

Now we need to replace words in our story:

lines = []
lines.append( "\n Now for the story! \n")
lines.append( "Once upon a time there was a %s with 2 weak arms." % Noun1)
lines.append( "The dog was very small and frail.")
lines.append( "One day a mean %s spoke to the dog and said..." % Noun2)
lines.append( "'You are too weak.  You must eat protein %s!'" % Noun3)
lines.append( "The dog was annoyed, but he started excercising daily.")
lines.append( "Pretty soon, he became stronger and more muscular!")
lines.append( "Now the dog shows off his %s on his own workout videos sponsored by P-89-X!" % Noun4)
lines.append( "The End!")

Finally we need to feed each line into the TTS engine. 

for line in lines:
	engine.say(line, line)
engine.runAndWait()

And Boom! We have a 'rudimentary' (That is the word I was searching for) madlib.  In the next tutorial, we will think of ways to get more definitions for other parts of speech.  See you in the next tutorial.

skip_nextCustom Def #3: Using Multiple Definitions
  • Custom Def #1: Import pyttsx3

  • Custom Def #2: Read a story from lines.
    (currently viewing)
  • 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...