Sometimes it’s nice to be able to provide output by speech rather than via the traditional console or some type of GUI. Text-to-speech with Python is actually a lot easier than you might expect.
I love Python, it is definitely my favorite and go-to language for smaller programs, or scripts. Python comes with many handy and easily available libraries and in this article, we are going to look at how we can output text-to-speech with Python. Depending on your OS it’s easier, than others so we will start out by looking at an example with MacOS where it’s arguably the easiest as no external libraries are required to be downloaded as MacOS provides a library called subprocess where text-to-speech is one of the functionalities.
We will use the subprocess library to output text to speech, it is extremely easy to get going.
It’s that simple! However, sadly, not all operating systems make it as easy, so next, we are going to look at a library that is available cross-platform.
In order to get it working on other platforms, we are going to need to install an external module called pyssx3 which is a library that works for both Python2 and Python3.
If you are new to Python and haven’t used pip before, I recommend reading up a bit on that before. It is a powerful package index for Python that makes it easy to find and install external modules that we later can import into our programs.
pip install pyttsx3
And then it’s as easy as the following to output text-to-speech with Python.
engine = pyttsx3.init();
With no native OS support for and having to resort to external libraries more errors can sometimes occur. Below are a couple of known errors and solutions that have worked for others.
Known errors and possible solutions
ModuleNotFoundError: No module named ‘Foundation’
– pip install pyobjc
No module named win32com.client
No module named win32
No module named win32api
– pip install pypiwin32
We have looked at how we can leverage text-to-speech on different operative systems. It really is surprisingly easy to do this with Python and you really can do some cool and fun stuff with it. So, why don’t you give it a try, and perhaps write in the comments below if you come up with some fun ideas to use it for.
Personally, I wrote a little program for my girlfriend that monitors a folder where pictures with text end up. When the program detects a new picture it reads the text inside the images and outputs it via text-to-speech with Python.