Last modified: Apr 16, 2026 By Alexander Williams

Python Text to Speech: Convert Text to Audio

Python makes it easy to convert text into spoken audio. This is called text-to-speech (TTS). You can use it for many projects. It can help make applications more accessible. It can also automate voice responses.

This guide will show you how to use Python for TTS. We will cover popular libraries. You will learn to create simple scripts. These scripts will turn written words into sound.

Why Use Python for Text to Audio?

Python is a great choice for TTS. It has simple syntax. Many powerful libraries are available. They are often free and open-source.

You can use TTS for many things. Create audiobooks from text files. Build voice assistants. Make educational tools that read content aloud. The possibilities are vast.

Using Python for this task is efficient. You can integrate it into larger applications. It works on different operating systems. This makes your projects flexible.

Popular Python Libraries for Text to Speech

Several libraries can handle TTS in Python. Each has its own strengths. We will focus on two main ones.

The first is pyttsx3. It works offline. It uses the speech engines already on your computer. This makes it very reliable.

The second is gTTS (Google Text-to-Speech). It uses Google's online service. It often has more natural-sounding voices. But it requires an internet connection.

Choosing between them depends on your needs. Need offline functionality? Use pyttsx3. Want high-quality online voices? Use gTTS.

Getting Started with pyttsx3

Let's start with pyttsx3. First, you need to install it. Use the pip package installer.


pip install pyttsx3
    

Now, let's write a basic script. This script will make your computer speak.


# Import the pyttsx3 library
import pyttsx3

# Initialize the TTS engine
engine = pyttsx3.init()

# The text you want to convert to speech
text_to_speak = "Hello, welcome to the world of Python text to speech."

# Use the say method to queue the text
engine.say(text_to_speak)

# Run and wait for the speech to finish
engine.runAndWait()
    

Run this script. You should hear the sentence spoken aloud. The init() function starts the engine. The say() method adds text to the queue. The runAndWait() method processes the queue.

You can customize the voice. You can change the speed and volume. Here is how.


import pyttsx3

engine = pyttsx3.init()

# Get details of current voice
voices = engine.getProperty('voices')

# Set a different voice (often index 1 is a female voice)
engine.setProperty('voice', voices[1].id)

# Change the speed of speech (default is 200)
rate = engine.getProperty('rate')
engine.setProperty('rate', rate - 50) # Slower speech

# Change the volume (between 0.0 and 1.0)
engine.setProperty('volume', 0.9)

engine.say("This is a customized voice message.")
engine.runAndWait()
    

Using gTTS for Online Text to Speech

Now, let's try gTTS. It creates an audio file. You can then play this file. First, install the library.


pip install gtts
    

You also need a player. For simple playback, install playsound.


pip install playsound
    

Here is a basic example with gTTS.


# Import the required libraries
from gtts import gTTS
import os

# The text to convert
my_text = "Python can easily convert this text into an audio file."

# Create a gTTS object
# 'lang' sets the language (en for English)
tts = gTTS(text=my_text, lang='en', slow=False)

# Save the audio as an MP3 file
tts.save("welcome_message.mp3")

# Play the saved file (method depends on your OS)
os.system("start welcome_message.mp3")  # For Windows
# os.system("mpg321 welcome_message.mp3")  # For Linux
# os.system("afplay welcome_message.mp3")  # For Mac
    

The gTTS() function creates the audio object. The save() method writes it to a file. You can then play it. The voice quality is usually very good.

You can change the language. Use different language codes like 'es' for Spanish or 'fr' for French.

Saving Speech to an Audio File

Sometimes you need a file, not just playback. Both libraries can do this. With pyttsx3, you use the save_to_file() method.


import pyttsx3

engine = pyttsx3.init()
engine.save_to_file("This text will be saved to a file.", "output_pyttsx3.mp3")
engine.runAndWait()  # This call is necessary to process the save command
print("Audio file saved successfully.")
    

With gTTS, you already saw the save() method. Saving files is useful. You can create podcasts or audio notes automatically.

Practical Applications and Project Ideas

Now you know the basics. What can you build? Here are some ideas.

Create a script that reads your emails aloud. Build a simple audiobook generator from text files. Develop a voice reminder system for your calendar.

You can combine TTS with other Python skills. For instance, use web scraping to get news articles. Then convert the article text to audio for a daily briefing.

Another idea is a language learning tool. It can pronounce words in a foreign language. This helps with listening practice.

Common Issues and Troubleshooting

You might run into problems. Here are common fixes.

Pyttsx3 might not find a voice engine on your system. On Linux, you may need to install espeak or festival. On Windows, it usually works out of the box.

gTTS requires an internet connection. If it fails, check your network. Also, Google might block requests if you make too many too quickly.

Audio playback might not work. Ensure you have a media player installed. The playsound library can help cross-platform.

If you get encoding errors with text, ensure your string is a proper Unicode string. You can add `u` before the string or use `.encode('utf-8')` if needed.

Conclusion

Converting text to audio in Python is straightforward. Libraries like pyttsx3 and gTTS do the heavy lifting. You can start with just a few lines of code.

Remember the key difference. Pyttsx3 works offline. gTTS uses online services for higher quality. Choose based on your project's requirements.

You learned how to install, use, and customize these tools. You saw how to save audio files. You also got ideas for real projects.

Now it's your turn. Start experimenting. Try reading a text file aloud. Change the voice and speed. Build something useful and fun. The power to give your applications a voice is at your fingertips.