Learn How To Convert Text To Speech In Python

text to speech in Python-img

CONVERTING TEXT TO SPEECH IN PYTHON IS EASIER THAN YOU THOUGHT..

Ever wondered how those pdf readers works? Or perhaps you have once wished if you could just be listening to a text while you do other stuffs instead of reading?

Well wonder no more because at the end of this tutorial you’ll be able to code up a text to speech program yourself in a matter of minutes.

Meanwhile, the fun fact here is that we’ll be doing more than just writing a program for text to speech in python. We’ll learn about other great python modules, make the program to be multi-lingual, and even get to improve our coding and programming skills.

WHAT MODULES AND EXPERIENCE LEVEL DO WE NEED TO CONVERT TEXT TO SPEECH IN PYTHON?

The difficulty rate for this version of text to speech in python tutorial is beginner/medium, and what’s required is not that much, once you already know the basics of python like importing of libraries/modules, python scope, indentation, input, while loop, functions and a few other similar concepts, then this should be fun..

However, we are going to be using the following modules for this tutorial:

gtts
Wikipedia
Playsound
Time
Multiprocessing
OS

Meanwhile, If this happens to be your first time of using or hearing about the aforementioned modules, please do yourself some good by at least reading up the basics about them to know more about them, however, you can still understand the code without reading them up, if you have the basic knowledge of python.

OK! we’ve had enough foreplay, let’s dive into coding without further ado..Talk is cheap but action is priceless

LET’S CODE IT UP

First off, we start by importing the necessary modules for this project:

#let's import some necessary modules
from gtts import gTTS
import wikipedia as wk
from playsound import playsound
from time import sleep
import multiprocessing as mp
import os

So we have imported the necessary modules we need, to make this tutorial even more fun we have to write 3 functions, the main function that will convert the text to speech, 2 nested functions to make the program fun and easy to use, like I said earlier we will do more than text to speech conversion.

The first nested function will be used to clear the screen when necessary. While the second nested function will print out whatever we insert as ‘content’ in a fun way as if you are using some text graphic design tool.

Inside the main function we will set up a variable (printfun) which we’ll use to spawn off a new process while some codes are executing the same time. Now let’s code everything up, I’ll explain the code afterwards.

#let's import some necessary modules
from gtts import gTTS
import wikipedia as wk
from playsound import playsound
from time import sleep
import multiprocessing as mp
import os


def get_info():
    
    #a function to clear the screen which works with all operating system
    def clear_sc():
        clear = ''
        if os.name == 'nt':
            clear = 'cls'
        else:
            clear = 'clear'
            
        return os.system(clear)
    
    #lets get the input from user, search, display and play the information.
    while True:
    
        try:

            word = input('\n Enter the phrase you want to search for:\n')
            sen = input('\n Enter the number of sentences you need in your information data. Digits only please!:\n')
            
            language = input('\nPlease choose your language option from the following lists:\n de: German, hi: Hindi, en: English, es: Spanish, fr: French, ar: Arabic. Please use the abbreviation only')
            clear_sc()
            wk.set_lang(language)
            print(f'\n[❗] Sourcing the information for {word} Please hold on...')
            info = wk.summary(word, int(sen))
            print('Done with information sourcing ✅')
            clear_sc()
            
            print(f'\n[❗] Generating the audio for {word} Please hold on...')
            audio_info = gTTS(info, lang = language)
            audio_info.save(f'{word}.mp3')
            print('Done with audio generation ✅')
            clear_sc()

        except:
            print('Please make sure you entered the right information in the right manner, be sure your phrase is wikipedia-comaptible, read the instructions again and try again')
            continue
        else:
            print('All good✅')
            break
        
        
   
    #lets create a function to print something a fun way
    def fun_print(content):
        for letter in content:
            print(letter, end = '', flush = True)
            sleep(0.07)
    
    #let's create a variable which will be used to spawn off a new process to use the fun_print func
    printfun = mp.Process(target = fun_print, args = (info,))
    
    #lets activate printfun
    print(f'\nBelow is summarized information about {word} in a written format:\n\n')
    printfun.start()
    
    #let's play the information as an mp3
    playsound(f'{word}.mp3')
    
    
get_info()

TEXT TO SPEECH IN PYTHON CODES EXPLAINED..

One of the beautiful stuffs about a good code is that any good programmer can follow up without a written explanation. However am still gonna make some basic explanations for total newbies.

First off, the first nested function(clear_sc) was used to clear the screen when necessary, this particular function will work on most operating systems including windows, linux and MAC. Read up the os module if you need to.

We used the while loop and try/except to control the possible errors from our code user and keep asking for the right input from the user until they get it right.

We added extra languages on the program to help our users whom English is not their first language. However you can even add extra languages to the code as well, all you need to see all the available languages, for instance in the case of gtts, just go to your command line after pip installing gtts and type in gtts-cli –all.

The variable printfun, which used the multiprocessing module, was used to spawn off a new process that targeted the second nested function(fun_print) function which prints out texts in a fun way. The reason for this that we want the fun_print to be printing the information out while our main code reads out the same thing being printed out using playsound module. Sounds like fun huh?

In case the code takes too long to fetch information, try stopping the code and change the language to en(English) I have noticed some languages takes too long. Meanwhile not all keywords works with wikipedia. Talking about wikipedia you might want to checkout this tutorial.

Furthermore, if you happen to get any error like ‘module error’, check if you have installed that particular module, you can install all the modules using pip install. However you don’t need to install os and time, you only need to import them.

So after generating the audio for information sourced with wikipedia, we saved and played the sound back using playsound module. To see more codes from Techswitch on playsound module, check the following tutorials: Play music with python And How To Add And Play Sound In Python Codes.

Hope you had fun with this tutorial. However, If you happen to run into any puzzle or challenge on this tutorial, please don’t hesitate to write me, or leave a comment at comment section.

To automatically get the first hand information when the next post or more advanced text to speech in python tutorial is published, subscribe to Techswitch’s newsletter or join our new telegram channel.

Get an affordable and seamless python one on one training today from anywhere in the world, location is never a barrier, we have friendly learning tools to make your python programming training a worthwhile experience.

🤞 Don’t miss the tips!

We don’t spam! Read more in our privacy policy

Geoff

Geoff is a python software engineer, web content specialist, tech private trainer and an IT virtual assistant.

This Post Has One Comment

Leave a Reply