Text-to-Speech Conversion in Python using pyttsx3

  • Post author:
  • Post comments:0 Comments
  • Reading time:29 mins read

What is pyttsx3?

pyttsx3 is a text-to-speech conversion library in Python. It allows you to synthesize text into audio using the built-in voices of your operating system.

To use pyttsx3, you will first need to install it using pip, the Python package manager:


pip install pyttsx3

Once pyttsx3 is installed, you can use it in your Python code as follows:


import pyttsx3

engine = pyttsx3.init()

engine.say("Hello, world!")

engine.runAndWait()

This will synthesize the text “Hello, world!” using the default voice on your system.

You can customize the voice that is used to synthesize the text by setting the voice options on the engine object. For example, to use a female voice, you can do the following:


import pyttsx3

engine = pyttsx3.init()

voices = engine.getProperty('voices')

engine.setProperty('voice', voices[1].id)

engine.say("Hello, world!")

engine.runAndWait()

You can also change the rate at which the text is spoken using the setProperty method. The rate is measured in words per minute. For example, to set the rate to 150 words per minute:


import pyttsx3

engine = pyttsx3.init()

engine.setProperty('rate', 150)

engine.say("Hello, world!")

engine.runAndWait()

Here is an example of how to use pyttsx3 to synthesize text from a file:


import pyttsx3

engine = pyttsx3.init()

with open("text_to_speech.txt", "r") as f:
    text = f.read()

engine.say(text)
engine.runAndWait()
Publisher
Latest posts by Publisher (see all)

Publisher

Publisher @ideasorblogs

Leave a Reply