Convert Text to Speech with Python in Different Languages

Convert Text to Speech with Python in Different Languages

In this tutorial, we will learn how to convert text into human-like speech.

This might be useful when you don’t want to read a document but want to listen to it instead. Also, some more advanced text-to-speech tools can be used to create a realistic voice for videos, ads, or podcasts.

To get started, let’s install the required module:

pip3 install gTTS pyttsx3 playsound

As you may guess, gTTS stands for Google Text To Speech, it is a Python library to interface with Google Translate’s text to speech API. It requires an Internet connection, and it’s pretty easy to use.

# Import the gTTS module for text to speech conversion 
# and playsound for playing the converted audio file
from gtts import gTTS
from playsound import playsound


# Here we are entering a text that we want to convert into audio
text_val = input('Enter or paste the text you want to be converted: ')

# Here we are choosing the language
language = input('''Choose the language:  
    EN for English
    ES for Spanish
    DE for German
    IT for Italian
    FR for French
    JA for Japanese
    PT for Portuguese
    HR for Croatian
''').lower()

# Choosing a file name for the new converted audio file
file_name = input('Please name your file:')

# Passing the text and language to the engine
obj = gTTS(text=text_val, lang=language)

# Here we are saving the transformed audio file
obj.save(file_name+'.mp3')

# Playing the converted audio file
playsound(file_name+'.mp3')

Here are the supported languages:

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

Leave a Reply

Prev
Extract PDF to Text File With Python
Extract PDF to Text File With Python

Extract PDF to Text File With Python

Today we are going to create a PDF to Text extractor

Next
Python LOOPS Exercise for Beginners and Intermediate

Python LOOPS Exercise for Beginners and Intermediate

Enable the edit mode with double click on the editor

You May Also Like