Text to speech is the generation of speech synthesized from the text. The technology is used to communicate with users when reading a screen is not possible or impractical. This not only opens up apps and information to use in new ways but can also make the world more accessible to people who cannot read text on a screen. In this article, I will take you through building your TTS with Python.
The technology behind the TTS has evolved over the past few decades. Using natural language processing, it is now possible to produce very natural speech that includes changes in pitch, speed, pronunciation and inflexion.
Text to Speech with Python
I will simply start with importing all the necessary libraries that we need for this task to create a program that takes the text of an article online and converts it into speech:
#Import the libraries
from newspaper import Article
import nltk
from gtts import gTTS
import os
Now, if you have face any error in importing the libraries, then you must have not installed any of these libraries. You can easily install any library in python, by writing a very simple command in your terminal – pip install package name. Now after installing and importing the libraries, we need to get an article from online sources so that we can create a program to convert text to speech from that article:
#Get the article
article = Article('https://hackernoon.com/future-of-python-language-bright-or-dull-uv41u3xwx')
Now, let’s download and parse the article:
article.download()
article.parse()
nltk.download('punkt')
article.nlp()
#Get the articles text
mytext = article.text
Now we have to choose the language of speech. Note “en” means English. You can also use “pt-br” for Portuguese and there are others:
language = 'en' #English
Now we need to pass the text and language to the engine to convert the text to speech and store it in a variable. Mark slow as False to tell the plug-in that the converted audio should be at high speed:
myobj = gTTS(text=mytext, lang=language, slow=False)
Running Text to Speech Program
myobj.save("read_article.mp3")
Now let’s play the converted audio file from text to speech in Windows, using the Windows command “start” followed by the name of the mp3 file:
os.system("start read_article.mp3")
Output
I hope you liked this article on converting text to speech using python. Feel free to ask your valuable questions in the comments section below.