Converting Text to Audio with Python: A Step-by-Step Guide

Converting Text to Audio with Python: A Step-by-Step Guide

In this tutorial, we'll explore how to convert text to audio using Python. This can be a handy feature for various applications, including creating audiobooks, generating voiceovers, or implementing accessibility features in your projects.

Prerequisites

Before we get started, make sure you have Python installed on your machine. You'll also need to install a few libraries. Open your terminal or command prompt and run the following commands:

pip install pyttsx3
pip install pydub

Step 1: Setting Up the Environment

Create a new Python script (e.g., text_to_audio.py) and open it in your favorite text editor or integrated development environment (IDE).

Step 2: Writing the Conversion Code

import pyttsx3
from pydub import AudioSegment
from pydub.playback import play

def text_to_audio(text, save_path="output.mp3"):
    # Initialize the text-to-speech engine
    engine = pyttsx3.init()

    # Save the text to an audio file
    engine.save_to_file(text, save_path)
    engine.runAndWait()

    # Load and play the generated audio
    audio = AudioSegment.from_mp3(save_path)
    play(audio)

# Example usage
text = "Hello, welcome to the text-to-audio conversion tutorial."
text_to_audio(text)

Step 3: Running the Script

Save your script and run it using the following command:

python text_to_audio.py

You should hear the audio output, and an output.mp3 file will be generated in the same directory.

Step 4: Customizing the Voice

You can customize the voice by adjusting the properties of the pyttsx3 engine. For example:

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)  # Change index to select a different voice

Step 5: Adding a GUI (Optional)

If you want to create a graphical user interface (GUI) for your text-to-audio converter, you can use a library like Tkinter. Here's a simple example:

import tkinter as tk
from tkinter import filedialog

def text_to_audio_gui():
    text = text_input.get("1.0", tk.END)
    text_to_audio(text)

# Create a Tkinter window
window = tk.Tk()
window.title("Text to Audio Converter")

# Add a text input field
text_input = tk.Text(window, wrap="word", width=40, height=10)
text_input.pack(pady=10)

# Add a button to trigger the conversion
convert_button = tk.Button(window, text="Convert", command=text_to_audio_gui)
convert_button.pack(pady=10)

# Run the Tkinter event loop
window.mainloop()

Conclusion

In this tutorial, we've covered the basics of converting text to audio using Python. You can further enhance and customize the functionality based on your project requirements. Feel free to experiment with different voices, add more features to your GUI, or integrate the converter into a larger application.

Happy coding😍🚀!

I have a more Advanced version of the code here!

Did you find this article valuable?

Support mehdi mediani by becoming a sponsor. Any amount is appreciated!