Creating Text to Speech App using Python



  1. First, install the Pyttsx3 library using pip by running the command "pip install pyttsx3" in your terminal.

  2. Next, import the library in your Python script using the following line of code:

import pyttsx3
  1. Initialize the TTS engine by creating an instance of the pyttsx3.init() function:
engine = pyttsx3.init()
  1. Set the voice and rate of the TTS engine using the setProperty() method:
voices = engine.getProperty('voices'
engine.setProperty('voice', voices[1].id
engine.setProperty('rate', 150)
  1. Define a function that will take in a string of text as an argument and use the say() method to convert it to speech:

def speak_text(text): 
     engine.say(text) 
     engine.runAndWait()

  1. Finally, call the function and pass in a string of text as an argument to convert it to speech:
speak_text("Hello, this is an example of text-to-speech using Pyttsx3 in Python.")

The above code is a basic example of how to use the Pyttsx3 library to convert text to speech in Python. You can further customize the app by adding features like allowing user to select voice, rate, volume and other parameters. 

Comments