Adding TTS to your python projects.


I have been teaching myself the python coding language for the past few months. I have experimented with a tutorial here and there and learned much from the content made freely available on the internet. If you are like me, you are no stranger to the "Hello World" examples out there. Sadly, most of them are intended for a sighted user. As a blind programmer, I need a little more from my output. I have found 2 useful ways anyone can use to get their python projects to speak aloud text using a computer's native TTS engine. I am happy to share what I have learned with you!


This tutorial assumes that you have the latest version of python installed on your machine and that you know how to open a python terminal, either by command line (cmd) or the python terminal. It also assumes you have the latest version of pip(python installer package) If you do not, please visit python.org and get yourself set up. There are a million tutorials out there on how to do this. I recommend the Nathantech.net python tutorial as this is where I got my start. Thanks Nathan Smith!


This tutorial was written and tested using python 3.7 on windows 10.


The 2 python modules we will look at today are pyttsx3 and accessible-output2.


pyttsx3


Click here to leave our site to check out pyttsx3 on pypy.org

Operating systems: windows, mac osx and posix


Supports multiple TTS engines, including Sapi5, nsss, and espeak.


Command Line installation: pip3 instal pyttsx3

I found this useful in my early coding experiences with python. I like that it had cross-platform functionality and it took advantage of my computer's native text to speech engine.


accessible-output2


Click here to leave our site to check out accessible-output2 on pypy.org

Operating system: windows


Supports NVDA, JAWS, sapi5 and more.


This module provides TTS and braille support.


command line installation: pip3 install accessible_output2


While working with a native TTs engine is nice and anything is better than nothing, I still wanted something that would work with my screen reading software. I use N.V.D.A. a free and open-source screen reader for windows, available from NVAccess.org This one was hard to find, but I reached out to another developer, Rocky Waters who was nice enough to point me towards this wonderful module. Rocky Waters is a developer of accessible games. You can check out his work on his game page. Thank you Rocky Waters!


On with the show...


Once you have your chosen modules installed, we can move on to the fun stuff.


Part 1 - using talk instead of print.


The print function is one of the most commonly used functions in python. It is used in almost every python program. Our tutorial will replace the print function with one called talk, which will use print to display the text and either pyttsx3 or accessible-output2 to speak aloud the text.


Now, let's open python!


type the following and press enter. print("Hello world")


If you are using a screen reader, you may have heard it read aloud text from the terminal or you may not have. If you did, don't get too happy. Screen readers are good at reading aloud text from a terminal. But Cerulean, if my screen reader is already reading this stuff, why do I need this tutorial? The answer is simple, developers rarely release software which relys on input and output from a command line terminal. If you are using python to make a game, you may not want the terminal to appear at all. Using one of these methods wil help ensure your apps are speaking properly.


Now, for the rest of this tutorial, you may wish to work with notepad and use python via the command line. Please open your command terminal and navigate it to a directory where you can work with a new .py file


Type: notepad tts.py and press ENTER. You wil get a warning that this file does not exist. Go ahead and tell it to make the file. Once your .py file is open in notepad, you are ready to read on further.


As stated above, the print function only displays visual text on the screen. We want our programs to talk, so we will define a function called talk to take the place of the print function in our code.


Defining a custom talk function.


Here is a sample for the talk function, in both pyttsx3 and accessible-output2.


Select the sample for the module you are working with and enter it in tts.py file.


Sample code using pyttsx3:


Download source code

1. #a program to make a python project speak using pyttsx3


2. import pyttsx3


3. #imports pyttsx3 to your project.


4. engine = pyttsx3.init()


5. #makes an engine object and initializes it to use tts.


6. newRate = int(200)


7. #make a variable to hold the speaking rate. In this case, 200 words per minute.


8. engine.setProperty('rate', newRate)


9. # sets the speaking rate to newRate.


10. def talk(msg):


11. #defines a function named talk and passes a string variable msg.


12.  engine.say(msg)


13.  #passes your msg to the engine.

14.  engine.runAndWait()


15.  #free up the engine to await more text.


16.  print(msg)


17.  #Prints your msg to the terminal.


18. talk("Hello World! I am speaking aloud!")


Sample code using accessible-output2:


Download the source code

1. # a python program which uses Accessibleoutput2 to speak aloud printed text.


2. import accessible_output2.outputs.auto


3. def talk(msg):


4. #defines a function called talk which passes the msg argument. msg must be a string.


5.  o = accessible_output2.outputs.auto.Auto()


6.  # Creates an accessible output object called o. This object will automatically find your native TTS engine or Screen reader.


7.  o.output(msg)


8.  #Outputs msg to your TTS engine or Screen reader.


9.  print(msg)


10.  #Prints msg to the terminal to be read visually.


11. talk("Hello World! I am speaking aloud!")


Talk to me baby!


Save your work and return to the command terminal. Type python tts.py and press ENTER.


If you entered the code correctly, you heard your computer say "Hello World! I am speaking aloud!" Congratulations, you can now make any program speak!" If not, go back and take another look. Remember python is a case-sensitive tabbed language.


Part 2 - Asking for input


The input function is another useful function. It gets keyboard input from the user to use in various operations. Just as we replaced the print function with a custom talk function, we will replace the input function with a custom ask function.


regardless of your choice of pyttsx3 or accessible-output2, you can add this function to your code sample:


1. #a function to get user input using our custom talk function.


2. def ask(msg):


3.  talk(msg)


4.  answer = input(msg)


5.  return answer


Now that you have added the ask function, we can play with our code a little.


At the bottom of your code, Add the following 2 lines:


name = ask("What is your name?")


talk("Hello "+name+", it is nice to finally speak to you!")

Save your work and return to the command terminal. Type python tts.py and press ENTER.


Enter your name when asked and press ENTER.


Rejoice!


Conclusion


In this tutorial, you learned how to make a python project speak with a screen reader or a native TTS engine. You also learned how to gain input from the user and use it in your custom talk function.


I also hoped to demonstrate how you can replace most instances of the print function with our custom talk functions in your source code. you also learned that you can replace most input functions with our custom ask function to gain input from a user.


I hope you found this tutorial useful. This is my very first tutorial and I hope to have more in the future.


Please feel free to send me your feedback anytime by using the contact link at the bottom of this page.