Text To Speech Conversion Using Python

Text To Speech Conversion Using Python

Python is a powerful and adaptable language used for multi-purpose programming. There are several things you can do with Python, one of which is text to speech conversion.

This article will walk you through the process of converting text to speech using Python and Google’s Text-To-Speech service. Google Cloud Text-to-Speech turns text into human-sounding speech in over 100 voices and 20+ languages and dialects.

It delivers high-fidelity audio by combining revolutionary voice synthesis technology (WaveNet) with Google’s strong neural networks. With this simple API, you can develop lifelike interactions with your consumers, revolutionising customer service, device interaction, and other applications.

We’ll be using the Python library gTTS (Google Text-to-Speech) for this purpose.

(Google Text-to-Speech) is a Python library and command-line program that interfaces with Google Translate’s text-to-speech API. This function saves spoken data to a file, a bytestring, or for additional audio manipulation or stdout.

It features flexible pre-processing and tokenizing, as well as automatic retrieval of supported languages. The gTTS API supports several languages including English, Hindi, Tamil, French, German and many more.

Let’s being with installing the gTTS package first.

pip install gtts

After installation, you can run the following scripts.

Note that gTTS, which works wonderfully in Python, requires an online connection to function because it relies on Google to obtain audio data, therefore your machine must have an active internet connection.

Python Program to convert a text into speech

import os
from gtts import gTTS

Text = "I love Medium and Python programming Language"

print("please wait...processing")
TTS = gTTS(text=Text, lang='en-uk')

# Save to mp3 in current dir.TTS.save("voice_test.mp3")
# Plays the mp3 using the default app on your system
# that is linked to mp3s.

os.system("start voice.mp3")

Save the file and run it this should open your default music player app and play the audio.

But what if you wanted to turn a long string or document into speech? You could do that by saving the material to a text file and then converting it to speech as seen below.


Python program to Convert a text file into speech

import os
from gtts import gTTS

# You will need a text file named test.txt
# to play, put it in the current dir.

FLIST = open("test.txt", "r").read().replace("\n", " ")
print("please wait...processing")
TTS = gTTS(text=str(FLIST), lang='en-uk')

# Save to mp3 in current dir.TTS.save("voice.mp3")
# Plays the mp3 using the default app on your system# that is linked to mp3s.
print(FLIST)
os.system("start voice.mp3")

Save and run the file you should get the desired output.

Show 3 Comments

3 Comments

  1. Your blog is a breath of fresh air in the often stagnant world of online content. Your thoughtful analysis and insightful commentary never fail to leave a lasting impression. Thank you for sharing your wisdom with us.HABANERO88

  2. Your blog is a breath of fresh air in the often mundane world of online content. Your unique perspective and engaging writing style never fail to leave a lasting impression. Thank you for sharing your insights with us.HABANERO88

  3. Your blog has quickly become one of my favorites. Your writing is both insightful and thought-provoking, and I always come away from your posts feeling inspired. Keep up the phenomenal work! SLOT DANA GOPAY

Leave a Reply

Your email address will not be published. Required fields are marked *