Code That Speaks: A Beginner's Guide to pyttsx3 Text-to-Speech (TTS) in Python

Jeffrey MintahJeffrey Mintah
2 min read

Introduction:

After getting the hang of Python basics, I found myself wondering, "What's the next step?" I had played around with if and else conditions and similar concepts, but honestly, they didn't grab my attention.

Sure, I understood their importance as fundamental parts of programming logic, but they just didn't have that special something that would ignite my passion for exploring Python further. It wasn't until I stumbled upon pyttsx3 that I truly saw how coding could become fascinating and captivating. That's when things took a thrilling turn!

Let's talk about this amazing library and all the cool things it can do!

pyttsx3

Pyttsx3 is a python package offering a cross-platform abstraction for Text-to-Speech (TTS) engines. It simplifies the process for developers to convert text into speech within their Python projects. This article provides a stepping stone for this package.

Requirements:

  • Python 3.xx

  • pyttsx3

Getting Started with pyttsx3:

To get started with pyttsx3, you can install the package using pip, the python package manager

pip install pyttsx3

Once installed, you can let your codes speak with a few lines of codes. Below is a python script demonstrating the usage of pyttsx3

Details of the script

import pyttsx3 as speech

engine = speech.init()
text = "I love Python programming"
engine.say(text)

engine.runAndWait()

  • We import pyttsx3 as speech to have access to all of its functionalities

  • The engine is initiated with the init function

  • A sample test is provided in the script and passed as a parameter in the say function

  • runAndWait is called to wait for our script to start rolling

Customizing Speech Output

With pyttsx3, you can tweak different aspects of speech like voice, speed, and volume.

#sets the speed of the voice
engine.setProperty('rate', 140)

#sets the volume 
engine.setProperty('volume', 0.8)

#this helps to set the gender voice to use
# voices[0] for a male voice and voice[1] for a female voice

voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)

Wrap Up

pyttsx3 is a powerful package that allows developers to create more engaging and interesting apps. This is a Beginner's guide of how to use this amazing package. Stay tuned as this guide will be regularly updated to delve into further possibilities and applications in various projects. Click Here to join in and let's code together!

1
Subscribe to my newsletter

Read articles from Jeffrey Mintah directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jeffrey Mintah
Jeffrey Mintah

An adept developer proficient in Python and various frameworks, I possess a fervent dedication to crafting resilient and highly efficient applications. My ambition is to become a proficient mobile app developer, driven by a passion for creating innovative and user-friendly solutions.