How to write a Text-To-Speech Python Program. (GTTS)

Text to Speech using Python Programming Language (Not Official Snippet)

We will start off by importing a library called “GTTS”. It stands for Google’s Text To Speech.

For that you’ll need to install this library by using your CMD. Go to your CMD and type:

pip install gtts

and hit enter. This will install the GTTS library in your system.

Now, open your IDE. In my case I am using PyCharm.

Create a new Python file, and import GTTS library using this code:

from gtts import gTTS

Now, we will also import OS as we are going to manipulate file a bit. So import OS using simple code:

import os

This should look like this:

from gtts import gTTS
import os

Okay, so we are done with the import part of code now let’s do some real coding.

Take a variable which will store our sentences as a string. In this case, it’s sentence.

Sentences = input("Write your sentences here: ")

Now setup GTTS variables:

language = 'en-us'
slow_audio_speed = False
filename = 'Audio.mp3'

You can choose between various set of language type. In English alone you get various types like “en-uk”, us, in, and so on. List is given below:

LIST GIVEN IN THE END OF ARTICLE ! 

Now we can setup our Audio and Allow Python to create a file by using this code snippet which explains itself.

audio_created = gTTS(text=Sentences, lang=language,slow=slow_audio_speed)
audio_created.save(filename)

Now we will make Python run the Audio Created using this code:

os.system(f'start {filename}')

That’s the end. Whenever you’ll run this program it’ll ask for some sentences and later it’ll give those sentences voice. In the in your code should look something like this:

from gtts import gTTS
import os
Sentences = input("Write your sentences here: ")
language = 'en-in'
slow_audio_speed = False
filename = 'Audio.mp3'
audio_created = gTTS(text=Sentences, lang=language,slow=slow_audio_speed)
audio_created.save(filename)
os.system(f'start {filename}')

Hope you like this Article about GTTS…

Make you follow me and also go through my social media handles where I am extremely active. Follow for more content like this.

List of Languages in GTTS:

# Chinese
'zh-cn': 'Chinese (Mandarin/China)',
'zh-tw': 'Chinese (Mandarin/Taiwan)',
# English
'en-us': 'English (US)',
'en-ca': 'English (Canada)',
'en-uk': 'English (UK)',
'en-gb': 'English (UK)',
'en-au': 'English (Australia)',
'en-gh': 'English (Ghana)',
'en-in': 'English (India)',
'en-ie': 'English (Ireland)',
'en-nz': 'English (New Zealand)',
'en-ng': 'English (Nigeria)',
'en-ph': 'English (Philippines)',
'en-za': 'English (South Africa)',
'en-tz': 'English (Tanzania)',
# French
'fr-ca': 'French (Canada)',
'fr-fr': 'French (France)',
# Portuguese
'pt-br': 'Portuguese (Brazil)',
'pt-pt': 'Portuguese (Portugal)',
# Spanish
'es-es': 'Spanish (Spain)',
'es-us': 'Spanish (United States)'
'af': 'Afrikaans',
'ar': 'Arabic',
'bn': 'Bengali',
'bs': 'Bosnian',
'ca': 'Catalan',
'cs': 'Czech',
'cy': 'Welsh',
'da': 'Danish',
'de': 'German',
'el': 'Greek',
'en': 'English',
'eo': 'Esperanto',
'es': 'Spanish',
'et': 'Estonian',
'fi': 'Finnish',
'fr': 'French',
'gu': 'Gujarati',
'hi': 'Hindi',
'hr': 'Croatian',
'hu': 'Hungarian',
'hy': 'Armenian',
'id': 'Indonesian',
'is': 'Icelandic',
'it': 'Italian',
'ja': 'Japanese',
'jw': 'Javanese',
'km': 'Khmer',
'kn': 'Kannada',
'ko': 'Korean',
'la': 'Latin',
'lv': 'Latvian',
'mk': 'Macedonian',
'ml': 'Malayalam',
'mr': 'Marathi',
'my': 'Myanmar (Burmese)',
'ne': 'Nepali',
'nl': 'Dutch',
'no': 'Norwegian',
'pl': 'Polish',
'pt': 'Portuguese',
'ro': 'Romanian',
'ru': 'Russian',
'si': 'Sinhala',
'sk': 'Slovak',
'sq': 'Albanian',
'sr': 'Serbian',
'su': 'Sundanese',
'sv': 'Swedish',
'sw': 'Swahili',
'ta': 'Tamil',
'te': 'Telugu',
'th': 'Thai',
'tl': 'Filipino',
'tr': 'Turkish',
'uk': 'Ukrainian',
'ur': 'Urdu',
'vi': 'Vietnamese',
'zh-CN': 'Chinese'

--

--

--

We write to create Difference, get Enlighted and Enlighten others. Hope you’ll get the best from us… Check out some of our blogs !

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

3 Things You Should Learn to Start Your Career as a Junior Software Engineer

My Journey at TIIDELab to self Reliance 4.0

Blow up Your JUnit5 Tests with Permutations

Memento | Cheat Sheet

5 Tips to Follow in Building HTML Forms

how How To Build Progressive Web App: Best Examples & Tools For PWA

Uncover the B-Side helpful Pandas functions

Translating Documentation : An Introduction

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
Antive's

Antive's

We write to create Difference, get Enlighted and Enlighten others. Hope you’ll get the best from us… Check out some of our blogs !

More from Medium

Bootcamp week 1: Learning to be professional (again)

4. Master 🥇 (Path to follow in Computer Science Journey).

Bug hunting: Chance or Chore?

Are college courses useful in industry?