Dark mode logo
Last Updated:
Python text to speech using 3 lines of code

Python Text to speech, For beginners!

Introduction

Text-to-speech(commonly known as TTS) is the process of conversion of text into speech. We can use Python to generate human-like speech from text input.

We all have used the print() statement in Python to output a text in the output screen. But did you know that it is possible to speak this output out of your speakers with just three lines of code, without the need to learn anything complex?

Let's find out how to make Python speak.

π—’π˜ƒπ—²π—Ώπ˜ƒπ—Άπ—²π˜„

In order to perform the text-to-speech conversion, we can use an existing library in Python. There are many TTS libraries to get this job done, but the one we are using is called pyttsx3. Don't get terrified of the scary name because it is just the short form of 'Python text to speech version 3'.

π—¦π—²π˜π˜π—Άπ—»π—΄ 𝗨𝗽 π˜π—΅π—² π—˜π—»π˜ƒπ—Άπ—Ώπ—Όπ—»π—Ίπ—²π—»π˜

First we need to install pyttsx3 library in python.

For that just follow these steps:

For Windows

1. Go to start and type 'cmd'. Press enter.
2. The command prompt screen will pop up.
3. In the screen type pip install pyttsx3

For Linux

1. Open the terminal by pressing 'Ctrl + Alt + T'.
2. In the Linux terminal, type pip install pyttsx3.

For Mac

1. Open the terminal.
2. In the Mac terminal, type pip install pyttsx3.

Β 

We will see that the pyttsx3 module is being installed. Once completed, close the terminal. We now have all the environment set up.

π—œπ—Ίπ—½π—Ήπ—²π—Ίπ—²π—»π˜π—Άπ—»π—΄ π—§π—²π˜…π˜-π˜π—Ό-𝗦𝗽𝗲𝗲𝗰𝗡 𝗢𝗻 π—£π˜†π˜π—΅π—Όπ—»

Now, its time code. First, we need to import the pyttsx3 library. We can do that by importing pyttsx3 Now, we can initiate the pyttsx3 engine.

For windows

For Windows, we will use the 'sapi5' speech synthesis platform.

engine = pyttsx3.init('sapi5')

For Linux,

For Linux, we will use the 'espeak' speech synthesis platform, which is open-source.

engine = pyttsx3.init('espeak')

For macOs,

For mac users, we will use the 'nsss' speech synthesis platform.

engine = pyttsx3.init('nsss')

Now we can use the engine.say() function, which inputs the text we want it to say. engine.say('input_text') We have to ask the python to wait till the talk is complete. For that, we have to use the runAndWait function. engine.runAndWait() And just like that we can make our Python program speak.

Conclusion

To summarise, we can code the basic text-to-speech program as:

For Windows,

import pyttsx3
engine = pyttsx3.init('sapi5')
text = 'Type your test data here :)'
engine.say(text)
engine.runAndWait()

For Linux,Β 

import pyttsx3
engine = pyttsx3.init('espeak')
text = 'Type your test data here :)'
engine.say(text)
engine.runAndWait()

For MacOS,

import pyttsx3
engine = pyttsx3.init('nsss')
text = 'Type your test data here :)'
engine.say(text)
engine.runAndWait()


Hope you have fun with it! Feel free to ask your questions in the comment section.

Comments