Convert Text to speech using python With source Code

Convert text into speech using python

Introduction:

In this project, we will convert the text into speech using Python. It will be made possible by using the gTTS module in Python. It is a Python library and CLI tool to interface to Google Translate Text to speech API. Let’s get into this and make some text to get converted into speech in any language you want.

Installation: 

o install the GTTS (Google Text-to-Speech) module for Python, you can use the pip package manager by running the following command in your command line or terminal:

$ pip install gtts

This will install the latest version of the GTTS module on your system. You can then use it in your Python script by importing it with the following statement:

$ from gtts import gTTS

Explanation:

The first step is to import the module and with the help of “import” keyword, we will import “gTTS” & “oslibraries for converting the text and playing the audio respectively.

This project can be done in two ways. One, the text can be provided by the user itself in the program. Second, a file can act as text and the whole file can be converted into speech. Let’s discuss both ways.

First way:

For this, first of all, the user stores the text under any variable, and then they should set the language in which they want to hear. So, ‘my_text’ and ‘language’ are the two variables created in this program to store the text and language type. Here, we want the speech to be in the English language, for this, we have to use the language code “en” for it. One can use anything accordingly.

In the next step, we will pass the text to the gTTS engine using “gTTS(text=, lang=, slow=) “.

In this, the variable in which the text is stored must get passed, the lang is for language, and the slow parameter takes a bool value accordingly if the user wants the output audio to be fast or slow.

Now, “.save(filename)” will request the API to write the result to the file, and with the help of “os. system()” we will interact with our OS for the audio file to be played by passing “start <filename>” as a parameter to the function.

Source Code:

Get Discount on Top Educational Courses

Brand NameDiscount InformationCoupon Codes Link
Educative.io20% discount on Educative courses and plans
W3Schools20% discount on W3Schools courses
KodeKloud10% discount on KodeKloud courses and plans
GeeksforGeeks30% discount on GeeksforGeeks courses
Target Test Prep20% discount on Target Test Prep
Coding Ninjas₹5000 discount on Coding Ninjas courses
Skillshare40% discount on Skillshare
DataCamp50% discount on DataCamp
365 Data Science57% discount on 365 Data Science Plans
Get SmarterFlat 20% discount on Get Smarter courses
SmartKeedaFlat 40% discount on SmartKeeda courses
StackSocial20% discount on StackSocial courses
				
					# Importing the library
from gtts import gTTS
import os

# Providing the text
input_text = "Hello! Curious Coder Here. Find Coding Project at Codewithcurious.com"

# Setting the language
language = "en"

# Passing to gtts engine
voice = gTTS(text=input_text, lang=language, slow=False)

# Creating and saving the audio file
voice.save("demo.mp3")

# Playing the file
os.system("start demo.mp3")
				
			

Second way:

For this, there is only one change in place of passing the text, we will open the file in a read mode by using the “open(filename, mode)” function. In this, for reading mode, we will pass “r” as the second parameter.

				
					# Importing the library
from gtts import gTTS
import os

# Providing the text file
f = open("text.txt", "r")
# reading the file
input_text = f.read().replace("\n", " ")

# Setting the language
language = "en"

# Passing to gtts engine
voice = gTTS(text=input_text, lang=language, slow=False)

# Creating and saving the audio file
voice.save("txt.mp3")

# Playing the file
os.system("start txt.mp3")
				
			

From any of the ways, you can build up an interesting project i.e. Converting text to speech!

Find More Projects

10+ Free Cryptocurrency & Bitcoin Website Using HTML, CSS, JavaScript & PHP Introduction Hello all of you are welcome to today’s new …

Pet Food Shop Website Using Html CSS and JavaScript Introduction Hello coders, welcome to another new blog. Today in this article we’ll …

Educational Website using html CSS and JavaScript Introduction Hello friends, welcome to today’s new blog post. Today we have created a beautiful …

A Django-Based Gym Management System Introduction Managing a gym can be complicated because there are so many things to keep track of, …

how to create a video background with html CSS JavaScript Introduction Hello friends, you all are welcome to today’s new blog post. …

Auto Text Effect Animation Using Html CSS & JavaScript Introduction Hello friends, welcome to today’s new blog post. Today we have created …

Get Huge Discounts
More Python Projects