0
0
PythonProgramBeginner · 2 min read

Python Program to Convert Text to Speech

Use the pyttsx3 library in Python to convert text to speech with pyttsx3.init() to initialize, engine.say(text) to set the text, and engine.runAndWait() to play the speech.
📋

Examples

InputHello, how are you?
OutputThe program speaks: 'Hello, how are you?'
InputPython programming is fun!
OutputThe program speaks: 'Python programming is fun!'
Input
OutputThe program speaks nothing (no output sound).
🧠

How to Think About It

To convert text to speech, first choose a library that can speak text aloud. Then, initialize the speech engine, give it the text you want to hear, and finally tell it to play the speech. This process turns written words into spoken audio.
📐

Algorithm

1
Import the text-to-speech library.
2
Initialize the speech engine.
3
Provide the text input to the engine.
4
Run the engine to speak the text aloud.
💻

Code

python
import pyttsx3

def text_to_speech(text):
    engine = pyttsx3.init()
    engine.say(text)
    engine.runAndWait()

# Example usage
text_to_speech('Hello, how are you?')
print('Speech completed.')
Output
Speech completed.
🔍

Dry Run

Let's trace the example 'Hello, how are you?' through the code

1

Import pyttsx3

The program loads the pyttsx3 library to use its speech functions.

2

Initialize engine

engine = pyttsx3.init() creates a speech engine instance.

3

Set text to speak

engine.say('Hello, how are you?') queues the text for speech.

4

Run speech engine

engine.runAndWait() plays the spoken text aloud.

5

Print completion

The program prints 'Speech completed.' after speaking.

StepActionValue
1Import librarypyttsx3 loaded
2Initialize engineengine instance created
3Set text'Hello, how are you?' queued
4Run engineSpeech played aloud
5Print'Speech completed.'
💡

Why This Works

Step 1: Initialize speech engine

The pyttsx3.init() function creates an engine that can convert text to speech.

Step 2: Queue text for speech

The engine.say(text) method tells the engine what words to speak.

Step 3: Play the speech

The engine.runAndWait() method processes the queued text and plays the audio aloud.

🔄

Alternative Approaches

gTTS (Google Text-to-Speech)
python
from gtts import gTTS
import os

def text_to_speech_gtts(text):
    tts = gTTS(text=text, lang='en')
    tts.save('output.mp3')
    os.system('start output.mp3')  # Use 'open' on Mac, 'xdg-open' on Linux

text_to_speech_gtts('Hello, how are you?')
print('Speech completed.')
Uses online Google service, requires internet, saves audio file before playing.
pywin32 SAPI (Windows only)
python
import win32com.client

def text_to_speech_win(text):
    speaker = win32com.client.Dispatch('SAPI.SpVoice')
    speaker.Speak(text)

text_to_speech_win('Hello, how are you?')
print('Speech completed.')
Windows-only, uses built-in Microsoft speech API, no extra files.

Complexity: O(n) time, O(n) space

Time Complexity

The time depends linearly on the length of the text n because the engine processes each character to generate speech.

Space Complexity

Space is proportional to the text length n as the engine stores the text internally before speaking.

Which Approach is Fastest?

The local pyttsx3 engine is faster than online services like gTTS because it does not require network calls.

ApproachTimeSpaceBest For
pyttsx3O(n)O(n)Offline, fast local speech
gTTSO(n) + network delayO(n)High-quality speech, requires internet
pywin32 SAPIO(n)O(n)Windows-only, native speech
💡
Install pyttsx3 with pip install pyttsx3 before running the program.
⚠️
Forgetting to call engine.runAndWait() causes no speech to play.