Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Text To Speech | pyttsx module
Next article icon

There are several APIs available to convert text to speech in Python. One of such APIs is the Google Text to Speech API commonly known as the gTTS API. gTTS is a very easy to use tool which converts the text entered, into audio which can be saved as a mp3 file. The gTTS API supports several languages including English, Hindi, Tamil, French, German and many more. The speech can be delivered in any one of the two available audio speeds, fast or slow. However, as of the latest update, it is not possible to change the voice of the generated audio.

Installation

To install the gTTS API, open terminal and write

pip install gTTS

Example 1: Using Python OS

Python
# Import the required module for text# to speech conversionfromgttsimportgTTS# This module is imported so that we can# play the converted audioimportos# The text that you want to convert to audiomytext='Welcome to geeksforgeeks Joe!'# Language in which you want to convertlanguage='en'# 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 speedmyobj=gTTS(text=mytext,lang=language,slow=False)# Saving the converted audio in a mp3 file named# welcomemyobj.save("welcome.mp3")# Playing the converted fileos.system("start welcome.mp3")

Output

The output of the above program should be a
voice saying, 'Welcome to geeksforgeeks Joe!'

Example 2: Uisng Python Pygame

This works for any platform. Now we are all set to write a sample program that converts text to speech.

Python
# Import the required module for text# to speech conversionfromgttsimportgTTS# Import pygame for playing the converted audioimportpygame# The text that you want to convert to audiomytext='Welcome to geeksforgeeks!'# Language in which you want to convertlanguage='en'# 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 speedmyobj=gTTS(text=mytext,lang=language,slow=False)# Saving the converted audio in a mp3 file named# welcomemyobj.save("welcome.mp3")# Initialize the mixer modulepygame.mixer.init()# Load the mp3 filepygame.mixer.music.load("welcome.mp3")# Play the loaded mp3 filepygame.mixer.music.play()

Output

The output of the above program should be a
voice saying, 'Welcome to geeksforgeeks!'

Convert Text to Speech Using Python | Python

A

akhil15126
Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp