Build A Text To Speech Program Using Python

Image for post
Image for post

In this article you will learn how to build your very own Text To Speech (TTS) program using the Python programming language !

If you prefer not to read this article and would like a video representation of it, you can check out the YouTube Video below. It goes through everything in this article with a little more detail, and will help make it easy for you to start developing your own program. Or you can use both as supplementary materials for learning !

Start Programming:

I will start by stating what I want this program to do. This program takes text from an online article and converts it to speech.

#Description: This program takes text from an online article and converts it to speech

Next, import the libraries that we will need for this program.

#Import the libraries
from newspaper import Article
import nltk
from gtts import gTTS
import os

Now that we have the libraries imported, we need to get the article from online.

#Get the article

article = Article('https://hackernoon.com/future-of-python-language-bright-or-dull-uv41u3xwx')
Image for post
Image for post
https://hackernoon.com/future-of-python-language-bright-or-dull-uv41u3xwx

Download the article.

article.download()

Parse the article.

article.parse()

Download the ‘punkt’ package. This should be a one time download.

nltk.download('punkt')

Apply Natural Language Processing (NLP)

article.nlp()

Create a variable called ‘mytext’ and store the articles text in it.

#Get the articles text
mytext = article.text

Choose the language for speech. Note ‘en’ stands for English. You can also use ‘pt-br’ for Portuguese and their are others as well.

language = 'en' #English

Pass the text and language to the engine to convert the text to speech and store it in a variable called ‘myobj’. Mark slow as False to tell the module that the converted audio should have a high speed.

# 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)

Save the converted audio as a .mp3 file named ‘read_article’.

myobj.save("read_article.mp3")
Image for post
Image for post
read_article.mp3 file

Play the converted audio on Windows, using the Windows command ‘start’ followed by the name of the mp3 file.

# Playing the converted file
os.system("start read_article.mp3")

Conclusion and Resources

That is it, you are done creating your program to predict the price of bitcoin ! Thanks for reading this article I hope its helpful to you all ! If you enjoyed this article and found it helpful please leave some claps to show your appreciation. Keep up the learning, and if you like machine learning, mathematics, computer science, programming or algorithm analysis, please visit and subscribe to my YouTube channels (randerson112358 & compsci112358 ).

Again, if you want, you can watch and listen to me explain all of the code in my YouTube video.

Python is considered an easy high level language to learn. Considering it’s one of the fastest growing programming languages and a programming language many companies and computer science departments use, it is definitely a language you want to get familiar with. Get a comprehensive, in-depth introduction to the core Python language with this hands-on book. Based on author Mark Lutz’s popular training course, this updated fifth edition will help you quickly write efficient, high-quality code with Python. It’s an ideal way to begin, whether you’re new to programming or a professional developer versed in other languages.

Image for post
Image for post
Learning Python

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store