Audiobook in python | Only 6 line code

Hello friends how are you today i am very excited because in this post i am going to teach "Audiobook in python" which is very interesting project in Python  that can be made only using 6 line of codes. To create this project you don't need of any pre knowledge of Python because this project is very simple of code and i will explain each line of code to you in a very simple way.

Watch this video i have explained everything step by step to create this project.

If you are a computer science students or Teacher then this project "Audiobook in python" is very interesting  project for you because after learning this post you will able to create your own Audiobook with just 6 line of codes.

Now i am going to explain the code line by line

Line 1: import library

from gtts import gTTS 

This is the first line which used to import the library gtts. Here gtts stands for
g=Google
t=Text
t=To
s=Speech
Means we can say that this is the library which is used to convert Text into Speech

Line 2: Open file
file=open("myfile.txt") Here file is a variable which is using to store file , open() is a predefined function which is used to open file and myfile.txt is a file name which is stored in project folder.


Line 3: Read file
txt=file.read()
Here read() is a predefined function which is used to read a file.


Line 4: Language for audio file
language='en'
Here language is a variable and 'en' stands for English. If you want to create audio in Hindi then use 'hi'.


Line 5: Creating audio file
audio=gTTS(text=txt,lang=language,slow=False)
Here gTTS is a predefined function that takes three parameter 
1.text: Text that will be converted into audio
2.lang: Language for audio
3.slow: Speed for audio


Line 6: Saving audio file
audio.save("myaudio.mp3")


here save is a predefined function which is used to save audio file. myaudio is the name of audio file and mp3 is the extension of audio file. 

Now i am going to write the complete code of this project

#import library
from gtts import gTTS
#open file
file=open("myfile.txt")
#read file
txt=file.read()
#setting language for audio
language='en'
#creating audio file of text
audio=gTTS(text=txt,lang=language,slow=False)
#saving audio file
audio.save("myaudio.mp3")

How to Run this code

Step 1: First install Python into your system. Click here link to install Python

Step 2: Install Pycharm to code in Python. Click here to install Pycharm
It is not compulsory for you to use Pycharm IDE for python if you are familier with python then you can use any IDE that supports Python.

Step 3: Create project with name like "MyAudioProject" and inside this project create a Python file with name "Program". If you have any doubt then watch the above video of this project.

Step 4: Create a Text file inside your project and write some sentence that you want to convert in Audio file and put the file name "myfile.txt".

Step 5: Now install gtts library. To install this library click on terminal and type "pip install gtts" and press enter.

Step 6: Now open the Program.py file and  paste the above code into it.

Now its time to execute python code, just execute python file and wait for seconds that IDE will take to convert Text into Audio. After execution of this file go to project folder you will get a Audio file with name "myaudio.mp3", just play this audio file and Enjoy.

I hope now you can create "Audiobook in python". If you have any doubt regarding this post  or you want something more in this post then let me know by comment below i will work on it definitely.


Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

If you like my post then share it with your friends. Thanks­čśŐHappy Coding.

Post a Comment

0 Comments