- Theekshana
- Posts
- How I Built a Python Script That Turns Any Text into Speech (And You Can Too in 5 Minutes!)
How I Built a Python Script That Turns Any Text into Speech (And You Can Too in 5 Minutes!)
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-by-Step Guide:
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
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()
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
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()
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()
Complete Code: Python Text-to-Speech Script
If you're feeling a bit lazy (like me) and just want to get straight to the code, here it is! You can copy and paste this into your Python environment to start using the text-to-speech functionality right away.
import pyttsx3
# Create the engine that will do the talking (so we don’t have to)
engine = pyttsx3.init()
# 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
# 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()
# Save the speech to a file for when you need a pep talk later
engine.save_to_file(text, 'output_audio.mp3')
engine.runAndWait()
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!
Reply