Skip to content
Related Articles

Related Articles

Python | Text to Speech by using pyttsx3
  • Difficulty Level : Hard
  • Last Updated : 03 Oct, 2019

pyttsx3 is a text-to-speech conversion library in Python. Unlike alternative libraries, it works offline and is compatible with both Python 2 and 3. An application invokes the pyttsx3.init() factory function to get a reference to a pyttsx3. Engine instance. it is a very easy to use tool which converts the entered text into speech.
The pyttsx3 module supports two voices first is female and the second is male which is provided by “sapi5” for windows.
It supports three TTS engines :

  • sapi5 – SAPI5 on Windows
  • nsss – NSSpeechSynthesizer on Mac OS X
  • espeak – eSpeak on every other platform

Installataion
To install the pyttsx3 module, first of all, you have to open the terminal and write

pip install pyttsx3


If you receive errors such as No module named win32com.client, No module named win32, or No module named win32api, you will need to additionally install pypiwin32.
It can work on any platform. Now we are all set to write a program for conversion of text to speech.

Code : Python program to convert text to speech




# Import the required module for text  
# to speech conversion
import pyttsx3
  
# init function to get an engine instance for the speech synthesis 
engine = pyttsx3.init()
  
# say method on the engine that passing input text to be spoken
engine.say('Hello sir, how may I help you, sir.')
  
# run and wait method, it processes the voice commands. 
engine.runAndWait()

Output :
The output of the above program would be a voice saying,

'Hello sir, how may I help you, sir.'

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :