Open In App

Text to Speech by using ttsvoice - Python

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

TTSVoice transforms written text into spoken language. TTSVoice Python library analyses text using natural language processing algorithms to produce synthetic speech that mimics human speech. Applications for this technology range from language translators to digital assistants like Siri and Alexa, to assistive solutions for those with visual impairments.

Required Modules:

pip install ttsvoice

Convert Text-to-Speech in Python using ttsvoice

The voice Python packages provide the tts() function to implement text-to-speech functionality.

Note: Run the codes in the Visual Studio IDE.

Syntax: tts(text,voice,tempo)

Parameters of tts() function:

  • text: Pass any python string or python string variable as the first argument. The text parameter is mandatory.
  • voice: Pass "male" or "female" to change the pitch of the voice according to gender. It is an optional parameter. The default parameter is "female".
  • tempo: Pass "high", "normal" or "low" to change the rate of words per 1 second. It is an optional parameter. The default parameter is "normal".

Example 1:

Python3
from ttsvoice import tts

tts("Hello from GFG")

Output : 

This code will turn the text "Hello from GFG" into speech. By default, if we are not 
giving any parameters, then the voice will be female and the tempo would be normal.

Example 2:

Python3
from ttsvoice import tts

tts("Hi from GFG","male" )

Output :

This code will turn the text "Hi from GFG" into speech. Here we are giving the voice 
should be Male, and the tempo is normal by default.

Example 3:

Python3
from ttsvoice import tts

tts("Hi from GFG","male","low")

Output :

This code will turn the text "Hi from GFG" into speech. Here we are giving the voice 
should be Male, and the tempo is low,so you will listen the text very slowly.

Example 4:

Python3
from ttsvoice import tts

tts("Hi from GFG","male","high")

Output:

This code will turn the text "Hi from GFG" into speech. Here we are giving the voice 
should be Male, and the tempo is High, so you will listen to the text very fast.

Example 5:

You have to create a temp.txt file and you can write your text in it. With the help of ttsvoice we can read every line of a text file.

Python3
from ttsvoice import tts
file1 = open("temp.txt","r")
aa=file1.readlines()
for a in aa:
    tts(a,"male")

Output:

The output we data from the text file. Here we are giving the voice should be Male, 
and the tempo is Normal.

Next Article
Article Tags :
Practice Tags :

Similar Reads