DEV Community

Cover image for How I Built a Python Script That Turns Any Text into Speech (And You Can Too in 5 Minutes!)
Theekshana Chamodhya
Theekshana Chamodhya

Posted on

How I Built a Python Script That Turns Any Text into Speech (And You Can Too in 5 Minutes!)

Introduction

Are you a lazy developer like me who loves automating everything and taking shortcuts? Well, you’re in for a treat! Imagine having Python do all the talking for you — literally. Whether you’re too lazy to read long articles or you want to build a virtual assistant to whisper sweet motivational quotes, I’ve got you covered. Today, I’m showing you how I built a Python script that turns any text into speech using the pyttsx3 library. And guess what? It’ll take less than 5 minutes!

Oh, and if you'd rather see this in action, I made a quick YouTube video walking through this exact process. Check it out here!

Why Text-to-Speech Is So Useful:

Text-to-speech technology has a wide range of applications, from improving accessibility for visually impaired users to generating voiceovers for content creators. You can even use it to create voice-enabled apps or personal assistants that can “talk” to you. And guess what? You don’t need to be a coding genius to make this work. By the end of this post, you’ll have your very own text-to-speech Python script that you can customize however you like!

Step 1: Install pyttsx3

First, we need to install the pyttsx3 library, which supports both offline and cross-platform speech synthesis.

Run this command in your terminal:

pip install pyttsx3
Enter fullscreen mode Exit fullscreen mode

Step 2: Import the Library and Initialize It

Now, let’s import pyttsx3 and initialize the text-to-speech engine in our Python script.

import pyttsx3

# Create the engine that will do the talking (so we don’t have to)
engine = pyttsx3.init()
Enter fullscreen mode Exit fullscreen mode

Step 3: Set Voice Properties (Optional)

You can customize the voice, rate, and volume of the speech. Here’s how:

# Let's slow it down a bit. Talking too fast is exhausting.
engine.setProperty('rate', 150)  # Default is 200

# Maximum volume because we like things LOUD.
engine.setProperty('volume', 1.0)

# # Changing voice to female (because variety is the spice of life)
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)  # voices[0] is male, voices[1] is female
Enter fullscreen mode Exit fullscreen mode

Step 4: Convert Text to Speech

Now, let’s make the magic happen. You can input any text and convert it to speech with this simple command:

# Give Python something ridiculous to say
text = "Hey you, stop scrolling and get back to work... Just kidding, you're doing great!"
engine.say(text)

# Tell the engine to actually do its job
engine.runAndWait()
Enter fullscreen mode Exit fullscreen mode

Boom! Your computer is talking now. You have successfully outsourced your reading to a robot. Congratulations, fellow lazy developer!

Step 5: (Optional) Save Speech to an Audio File

Want to save the generated speech as an audio file? You can do that too!

# Save the speech to a file for when you need a pep talk later
engine.save_to_file(text, 'output_audio.mp3')
engine.runAndWait()
Enter fullscreen mode Exit fullscreen mode

Use Cases You Never Knew You Needed:

  • Audiobooks for Procrastinators: Why read when you can have Python read to you?

  • Daily Affirmations: Set Python to tell you you’re a genius every morning.

  • Office Shenanigans: Make it say something funny and prank your coworkers.

  • Procrastination App: Every time you try to slack off, this script reads you a motivational quote.

Final Thoughts:

And there you have it! In just a few lazy minutes, you’ve turned Python into your personal narrator. Whether you’re using it to turn articles into audiobooks or just have it remind you how awesome you are, this script has endless possibilities. And it’s super customizable, so feel free to tweak it, add some flair, and go wild!

If you’re lazy like me and want the complete code ready to go, click here to get it directly from my Beehive newsletter.

Call to Action:

Enjoyed this lazy coding tutorial? Share it with your friends, and be sure to subscribe to my newsletter for more Python hacks and shortcuts. Oh, and let me know in the comments what fun things you’ve made Python say!

Top comments (0)