To convert text to speech in Python, you can use the gTTS API, or pyttsx3 library, which is a Text-to-Speech (TTS) engine that works offline. There are number of APIs available to convert text to speech in Python.
We will see both this approach to generate text to speech.
What is Text to Speech ?
Text-to-Speech (TTS) is a technology that converts written text into spoken words. It is also known as speech synthesis. TTS systems take textual input and generate artificial human-like speech as output. This technology has various applications and can be used in different scenarios. cool isn’t it.
Virtual voice assistants like Siri, Alexa, and Google Assistant use TTS to respond to users’ queries and provide information audibly.
Using gTTS API
It is the Google Text to Speech API commonly known as the gTTS API. gTTS is a very easy to use tool which converts the text entered, into audio which can be saved as a mp3 file in your folder.
The gTTS API also supports several languages including English, Hindi, Tamil, French, German and many more. First of all install this API to your system using terminal. Follow the below command.
pip install gTTS
Now start writing code.
Import the required module for text to speech conversion.
from gtts import gTTS
This below module is imported so that we can play the converted audio.
import os
Write the text that you want to convert to audio.
mytext = 'Welcome to DTI labs!'
Language in which you want to convert, here it is english.
language = 'en'
Passing the text and language to the engine, here we have marked slow=False. Which tells the module that the converted audio should have a high speed.
myobj = gTTS(text=mytext, lang=language, slow=False)
Saving the converted audio in a mp3 file named welcome.
myobj.save("welcome.mp3")
Playing the converted file.
os.system("mpg321 welcome.mp3")
That’s it, now you can run your favorite text. Full code is below:
from gtts import gTTS
import os
mytext = 'Welcome to DTI labs!'
language = 'en'
myobj = gTTS(text=mytext, lang=language, slow=False)
# Saving the converted audio in a mp3 file
myobj.save("welcome.mp3")
# Playing the converted file
os.system("mpg321 welcome.mp3")
Using pyttsx3
To convert text to speech in Python, you can use the pyttsx3 library, which is a Text-to-Speech (TTS) engine that works offline. First, you need to install the library using pip.
pip install pyttsx3
import pyttsx3
Write a function which will handle all code for text to speech. Converts text to speech and speaks it out loud. Parameters are, text (str): The text to be converted to speech, and rate (int, optional): Speed of speech. Default is 150.
def text_to_speech(text, rate=150):
# Initialize the TTS engine
engine = pyttsx3.init()
# Set the speech rate (speed)
engine.setProperty('rate', rate)
# Convert the text to speech
engine.say(text)
# Wait for speech to finish
engine.runAndWait()
Now call the function and give the value text.
if __name__ == "__main__":
text_to_speech("Hello, welcom to DTI labs!")
That’s it, this should be converted to speech.
Conclusion
Over the past few years TTS(text to speech) technology has significantly improved our lives, with more natural-sounding voices and better linguistic processing, making it an essential part of modern technology which benefiting a wide range of users. I hope you must have enjoyed this article. Let me know in comment box below. Thanks.