October 13, 2024

How to Convert Text to Speech in Python

Converting text to speech (TTS) in Python can be easily achieved using various libraries. One of the most popular libraries for this purpose is gTTS (Google Text-to-Speech), which provides a simple interface to convert text to speech using Google Translate’s TTS API. Below is a step-by-step guide on how to convert text to speech in Python using gTTS.

1. Install the gTTS Library

First, you need to install the gTTS library using pip.

Example:

pip install gTTS

This command will install the gTTS library, which you can use to convert text to speech.

2. Import the Necessary Libraries

Once the library is installed, you can import it into your Python script.

Example:

from gtts import gTTS
import os

3. Convert Text to Speech

You can convert text to speech by creating an instance of the gTTS class, passing the text you want to convert, and saving the output as an audio file.

Example:

# Text to convert
text = "Hello, welcome to the world of Python programming."

# Creating the gTTS object
tts = gTTS(text=text, lang='en', slow=False)

# Saving the converted audio in a mp3 file
tts.save("output.mp3")

# Playing the converted file
os.system("start output.mp3")

In this example:

  • The text "Hello, welcome to the world of Python programming." is converted to speech.
  • The gTTS object is created with the specified text, language ('en' for English), and speed (slow=False for normal speed).
  • The output is saved as an output.mp3 file.
  • The os.system("start output.mp3") command plays the audio file on Windows. For macOS or Linux, you might need to use different commands like os.system("afplay output.mp3") or os.system("mpg123 output.mp3").

4. Customizing the Speech

You can customize the speech by changing the language or adjusting the speed of the speech.

Example: Changing the Language

# Text to convert
text = "Bonjour, bienvenue dans le monde de la programmation Python."

# Creating the gTTS object with French language
tts = gTTS(text=text, lang='fr', slow=False)

# Saving the converted audio in a mp3 file
tts.save("output_french.mp3")

5. Using pyttsx3 for Offline Text-to-Speech

If you need an offline solution, you can use the pyttsx3 library, which works without an internet connection.

Installing pyttsx3:

pip install pyttsx3

Example:

import pyttsx3

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

# Text to convert
text = "This is an offline text to speech conversion."

# Converting text to speech
engine.say(text)
engine.runAndWait()