Text-To-Speech conversion in Python

In this tutorial, we’ll be learning about Text-To-Speech(TTS) conversion using a Python library named pyttsx3. We’ll write a message which our System will speak. This library has many advantages as it can work offline too, with no or very little delay in getting a response and it is compatible with both python 2 and python 3. So let us start learning, Firstly with the installation process.

Installing pyttsx3

Open your command prompt and type the following command.

pip install pyttsx3

This library is dependent on win32 for which we may get an error while executing the program. To avoid that simply install pypiwin32 in your environment.

pip install pypiwin32

We are done with our installation part and let’s move to write our first Text-To-Speech or TTS code.

Moving forward with pyttsx3: Text to speech

Go through the code below which is self-explanatory.

import pyttsx3    # importing the library

def my_speak(message):

    engine = pyttsx3.init('sapi5')
    # initialize the voice engine which will use the mentioned driver
    # other drivers supported 'nsss', 'espeak'

    voices = engine.getProperty('voices')
    # get the current value of the propery 
    # like voices, voice, rate, volume

    for voice in voices:
    # to get the info. about various voices in our PC 
       print("ID: %s" %voice.id)
       print("Name: %s" %voice.name)
       print("Age: %s" %voice.age)
       print("Gender: %s" %voice.gender)
       print("Languages Known: %s" %voice.languages)

       # set specific attributes to voice like voice.id, rate, volume.
       # voice.id[0] will corresponds to first voice 
       # voice.id[1] will corresponds to second voice and so on.
       # rate is the no. of words/minute in integer.default is 200.

    # say() to make the PC speak.

    # run the event loop untill the completion of the text message 

message=input("Enter your message : ")
# Enter the text we want PC to speak

# calling the function we have created
# with our message as an argument

These are some important commands of pyttsx3

  1. pyttsx3.init(driver_name) – It will initialize our TTS engine which will use one of the drivers mentioned below.
    • sapi5 – on Windows
    • nsss – on Mac OS X
    • espeak – on Ubuntu and others.
  2. getProperty(property_name) – This will return the value of the property_name of the voice and using this command we can also get the details of various voices in our system(shown in code using for loop).
  3. setProperty(‘property_name’, value) – This command is very useful in case we want to change the voice, the speed of speech and the volume of speech.
  4. say() – The text given to this function is the one which will be spoken by our system.
  5. runAndWait() – This function is very necessary because it prevents our engine to stop until the whole message is spoken.


Text-To-Speech conversion in Python

In the output, we can see that I have 2 voices named HAZEL and ZIRA on my PC supporting the English Language with Great Britain and the United States accent respectively. We want you to try this library and find out which languages are present on your system.

More with speech to text

We can use text-to-speech conversion in our projects and can also combine with speech recognition to make some fully voice-controlled systems. We recommend going through the tutorial below to know about speech-recognition.

Get voice input with microphone in Python using PyAudio and SpeechRecognition

That’s all for this tutorial. We hope you like this tutorial and if you have any doubts, feel free to leave a comment below.

Learn more with us:

Voice Command Calculator in Python using speech recognition and PyAudio

Find all the microphone names and device index in Python using PyAudio

Leave a Reply

Your email address will not be published. Required fields are marked *