How to Convert Text to Speech Using Python

How to Convert Text to Speech Using Python

Text-to-speech (TTS) technology has become increasingly popular for applications like voice assistants, audiobooks, and accessibility tools. Python makes it easy to implement TTS with just a few lines of code. In this guide, we'll explore how to convert text to speech using Python's most popular module, pyttsx3.

Why Use pyttsx3 for Text-to-Speech?

pyttsx3 is a widely used Python library for text-to-speech conversion. Here’s why it stands out:

  • Offline functionality: Unlike cloud-based APIs, it works without an internet connection.
  • Cross-platform: Supports Windows, macOS, and Linux.
  • Customisable: Adjust speech rate, volume, and voice selection.
  • Lightweight: No heavy dependencies or setup required.

Installing pyttsx3

Before we begin, ensure you have Python installed. Then, install pyttsx3 using pip:

pip install pyttsx3

Basic Text-to-Speech Conversion

Here’s a simple script to convert text to speech:

import pyttsx3

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

# Text to be spoken
text = "Hello, welcome to the world of Python text-to-speech."

# Convert text to speech
engine.say(text)

# Play the speech
engine.runAndWait()

Explanation:

  • pyttsx3.init() initialises the TTS engine.
  • engine.say() queues the text for speech.
  • engine.runAndWait() processes the queue and plays the speech.

Customising Speech Properties

You can modify speech rate, volume, and voice selection:

import pyttsx3

engine = pyttsx3.init()

# Get available voices
voices = engine.getProperty('voices')

# Set a female voice (if available)
engine.setProperty('voice', voices[1].id)

# Adjust speech rate (words per minute)
engine.setProperty('rate', 150)  # Default is 200

# Adjust volume (0.0 to 1.0)
engine.setProperty('volume', 0.9)

# Speak
engine.say("Customising speech is easy with pyttsx3.")
engine.runAndWait()

Key Properties:

  • Rate: Controls speed (default is 200).
  • Volume: Ranges from 0.0 (mute) to 1.0 (max).
  • Voice: Switch between male/female voices if available.

Saving Speech to an Audio File

Want to save the speech as an MP3 or WAV file? Here’s how:

import pyttsx3

engine = pyttsx3.init()

# Text to save as audio
text = "This audio will be saved to a file."

# Save to an MP3 file
engine.save_to_file(text, 'output.mp3')

# Run the engine to generate the file
engine.runAndWait()

Note: The output format depends on the system's audio drivers. Some platforms may only support WAV.


Conclusion

With pyttsx3, converting text to speech in Python is straightforward and highly customisable. Whether you're building a voice assistant, accessibility tool, or just experimenting, this library provides a reliable offline solution.

SEO Keywords: Python text-to-speech, pyttsx3 tutorial, convert text to audio Python, TTS Python offline, save speech to MP3 Python, customise speech rate Python.

Incoming search terms
- How to convert text to speech using Python
- Best Python library for text-to-speech
- Offline TTS Python example
- pyttsx3 save speech to MP3 file
- Change voice in Python text-to-speech
- Python script for converting text to audio
- How to adjust speech rate in pyttsx3
- Text-to-speech without internet in Python
- Simple TTS Python code example
- Customising voice properties in pyttsx3

No comments:

Post a Comment