Text to Speech Talker

Tags: tiny, beginner

This program demonstrates the use of the pyttsx3 third-party module. Any message you enter will be spoken out loud by your operating system’s text-to-speech capabilities. Although computer-generated speech is an incredibly complex branch of computer science, the pyttsx3 module provides an easy interface for it, making this small program suitable for beginners. Once you’ve learned how to use the module, you can add generated speech to your own programs.

More information about the pyttsx3 module can be found at https://pypi.org/project/pyttsx3/.

text_to_speech_talker.py
 1"""Text To Speech Talker, by Al Sweigart al@inventwithpython.com
 2An example program using the text-to-speech features of the pyttsx3
 3module.
 4View this code at https://nostarch.com/big-book-small-python-projects
 5Tags: tiny, beginner"""
 6
 7import sys
 8
 9try:
10    import pyttsx3
11except ImportError:
12    print('The pyttsx3 module needs to be installed to run this')
13    print('program. On Windows, open a Command Prompt and run:')
14    print('pip install pyttsx3')
15    print('On macOS and Linux, open a Terminal and run:')
16    print('pip3 install pyttsx3')
17    sys.exit()
18
19tts = pyttsx3.init()  # Initialize the TTS engine.
20
21print('Text To Speech Talker, by Al Sweigart al@inventwithpython.com')
22print('Text-to-speech using the pyttsx3 module, which in turn uses')
23print('the NSSpeechSynthesizer (on macOS), SAPI5 (on Windows), or')
24print('eSpeak (on Linux) speech engines.')
25print()
26print('Enter the text to speak, or QUIT to quit.')
27while True:
28    text = input('> ')
29
30    if text.upper() == 'QUIT':
31        print('Thanks for playing!')
32        sys.exit()
33
34    tts.say(text)  # Add some text for the TTS engine to say.
35    tts.runAndWait()  # Make the TTS engine say it.

https://inventwithpython.com/bigbookpython/project74.html