text-to-speech

Text to speech using Python | Create Your Own Audio Book 

Hello World… Today we will see how to convert text to speech using python.

Introduction :

In this article, we are going to create our own audiobook, which converts texts to speech. We can use a python package to convert the text to speech known as gTTS.

Project Prerequisites : 

We just need to install one package for this project.

gTTS is a short form of Google Text-to-Speech a python library and a command-line interface (CLI) tool to interface with gTTS API(application programming interface).

We will import the gTTS library from the gtts module which can be used for speech translation. The text variable is a string used to store the user’s input.

Let’s get started…

Open cmd prompt and install the gTTS package.

  • pip install gTTS

Above is the sample image of installing the package. You can check for reference. After running the above command, It takes 2-3 minutes to install all the files of the library.

To know more about the package, you can refer to this link : gTTS · PyPI

gTTS

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. http://gtts.readthedocs.org/

PyPI version
Python versions
Tests workflow
codecov

Features

  • Customizable speech-specific sentence tokenizer that allows for unlimited lengths of text to be read, all while keeping proper intonation, abbreviations, decimals and more;
  • Customizable text pre-processors which can, for example, provide pronunciation corrections;
See also  Python | QR code generator

Code Implementation : 

from gtts import gTTS #, We have imported this to convert the text to mp3 audio

s = input(“Enter the File name: “) # enter the txt file name

s = input("Enter the File name: ") # enter the txt file name

f = open(s)
text = f.read()

f = open(s)
text = f.read()

obj = gTTS(text= text, lang= ‘en’ ,slow= False) # en — english || You can change slow value into True..

obj = gTTS(text= text, lang= 'en' ,slow= False) # en -- english || You can change slow value into True..

f1 = input(“Enter the Audio name to be saved: “) # enter the audio file name that will be auto-generated.

f1 = input("Enter the Audio name to be saved: ") # enter the audio file name that will be auto-generated.

obj. save(f1) # audio file will auto-saved in the same directory

obj. save(f1) # audio file will auto-saved in the same directory

Source code :

from gtts import gTTS  # We have imported this to convert the text to mp3 audio

s = input("Enter the File name: ") # enter the txt file name

f = open(s)
text = f.read()

obj = gTTS(text= text, lang= 'en' ,slow= False) # en -- english || You can change slow value into True..

f1 = input("Enter the Audio name to be saved : ")  # enter the audio file name that will be auto generated.

obj.save(f1)  # audio file will auto saved in the same directory

Here.. sample.mp3 audio file automatically generated in the same directory.

Output :

You can run the source code and get the output like the following.

Output Audio File:

Note:

So, that’s it !! This is the simplest method to create your own audio book using python. You can make slight modifications in the code, regarding your interest, to increase the look of the output

See also  Convert any eBook into Audiobook: 12 lines of Python

Thank You!

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.