MULTILINGUAL TEXT TO SPEECH IN PYTHON
Tags: python, programming
Published: 2021-11-06 23:58:53

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

Fun fact is that you can actually do more than just coding a test to speech program, you'll able to make it multilingual even with a topic of your choice. Sounds right fun right?


##WHAT MODULES AND EXPERIENCE LEVEL DO YOU NEED TO CONVERT TEXT TO SPEECH IN PYTHON?
The difficulty rate for this program/codes 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 program:
[gtts](https://pypi.org/project/gTTS/)
[Wikipedia](https://pypi.org/project/wikipedia/)
[Playsound](https://pypi.org/project/playsound/)
[Time](https://docs.python.org/3/library/time.html)
[Multiprocessing](https://docs.python.org/3/library/multiprocessing.html)
[OS](https://docs.python.org/3/library/os.html)

Meanwhile, If this happens to be your first time of using or hearing about the aforementioned modules, please do yourself some good by reading up the basics about them(You can just click on the aforementioned modules), 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

![python text to speech-talk is cheap gify](https://media.giphy.com/media/3o6wr9O7eCSG3T1y80/giphy.gif)

First off, we start by importing the necessary modules for this short project:
```python
#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 the program 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.

```python
#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/phrases works with wikipedia. Talking about wikipedia you might want to checkout this [tutorial](https://pythgenie.geofsphere.com/research-assistant-program/).

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 Pythgenie on playsound module, check the following tutorials: [Play music with python](https://pythgenie.geofsphere.com/play-music-with-python/) And [How To Add And Play Sound In Python Codes.](https://pythgenie.geofsphere.com/play-sound-in-python/)

Some people do use other modules like pyttsx3 for text to speech and pywhatkit for information sourcing, but I personally like to use gtts since the voice doesn't terribly sound robotic like pyttsx3.

I hope you enjoyed the short project though, feel free to exploit the codes, add more codes and even make it better than mine.

You can also extract information/contents from a pdf file using PyPDF2 or any other possible source and use what you learnt here to create your own pdf reader or something similar, sounds great right?

Let me know what you think on the comment section. Visit [Pythgenie](https://pythgenie.geofsphere.com) for more codes like this. Thank you for reading...