Text-to-Speech Using Python Code

Learn Text to Speech using the pyttsx3 module & get its code.

    Learn to code through which your program will be smart enough to read what you write & not only read, your code will read it loudly for you. First, you need to know about some modules to make your code read/speak.

    What modules we will be using is pyttsx3 (read the documentation) from this module we can access the windows API that is responsible for the audio management. To install this module type in terminal or command prompt the following command:

 pip install pyttsx3

    Voices in Windows : 

  1. Mark (Male Voice)
  2. Zira (Female Voice)
  3. David (Male Voice)
    You can manually check from your settings goto Settings > Time & Language > Speech or you can go from Control Panel > Speech & Voices. Now import that module in your code
    
import pyttsx3

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voices', voices[0].id)

So now let me explain you this code, we have imported the module, then we initialize this module with a variable name as engine. The engine variable contains all the class, function that can be used to make our program speak, next we have set the voice using Windows API i.e SAPI5, each voice has an id with help of that id we can set voices. Once it's done we move on to the next step i.e make it speak with the help of the say() function.

engine.say("Hello My Name is Prathmesh, Welcome to my Blogspot from Google")
engine.runAndWait()

What this say() function does is make it able to speak and runAndWait() function collects all speakable data and runs before the control goes to runAndWait() function.

Follow for more such blogs, do post comments.





Comments