Home » Convert Text to Speech in Python using gTTS

Convert Text to Speech in Python using gTTS

Spring Framework Basics Video Course
Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Developer (Upgrade) [1Z0-817]
1 Year Subscription
Java SE 11 Programmer I [1Z0-815] Practice Tests
Oracle Java Certification

In this article we show you how to turn text to speech in python using gTTS. What is that ?

gTTS (Google Text-to-Speech), a Python library and CLI tool to interface with Google Translate's text-to-speech API. Write spoken mp3 data to a file, a file-like object (bytestring) for further audio manipulation, or stdout. Or simply pre-generate Google Translate TTS request URLs to feed to an external program

Table of Contents

Installation

To install  gTTS API, open a command prompt and enter the following

pip install gTTS

Code

 

# Import the module 
from gtts import gTTS
import os
  
# The text that you want to convert to audio
mytext = 'Welcome to maxpython!'
  
# Language in which you want to convert
language = 'en'
  
# Pass the text and language to the engine, 
myobj = gTTS(text=mytext, lang=language, slow=False)
  
# Saving the converted audio in a mp3 file named
myobj.save("test.mp3")
  
# Playing the converted file
os.system("test.mp3")

Now when you run this an mp3 file will be created and then played with your default player for mp3 files. On my system it was vlc

Link

http://gtts.readthedocs.org/

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More