Python is a powerful programming language. As we all know almost everything is possible in with python. Here I am demonstrating a simple python program to translate text into speech.

For this program, I am using a package pyttsx3. It needs only few lines of code to get the basic program running.

Install the dependent package with the command below.

pip install pyttsx3

A very basic program is given below.

import pyttsx3
engine = pyttsx3.init()
engine.say("Hello everyone. This is my first text to speech conversion")
engine.runAndWait()

In the above example, the text gets played in male voice. If you want female voice, make the following change and run the program again.

voices = engine.getProperty('voices') 
engine.setProperty('voice', voices[1].id)

The complete program with female voice is given below.

import pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say("Hello everyone. This is my first text to speech conversion")
engine.runAndWait()

If you want to save the speech to a file, use save_to_file() function instead of say() function. Program for saving voice to a file is given below. This will save the speech into an mp3 file.

import pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.save_to_file("Hello everyone. This is my first text to speech conversion","myvoice.mp3")
engine.runAndWait()
engine.stop()

The above code snippets are basic examples. You can modify this as per your requirements.