Text to Speech in Python using 6 line code only

Hello friends how are you, Today in this post "Text to Speech in Python" i am going to teach you how you can convert text of any file into speech using just 6 lines of code. If you are a computer science students then this may be a small project for you and you can submit this into your school or college. 

Now  i am going to explain it step by step so just go through this post to understand this completely.

If you want to understand this through video then watch this video i have explained it step by step live

Step 1: Install Python and Pycharm : Open any browser and type Download Python and click the first link you will get official website of python here you will get a Download button and after clicking on this button you will get exe of latest python version just install it into your system.

To install Pycharm IDE Open any browser and type Download Pycharm and click the first link you will get official website of Pycharm  here you will get a black download button and after clicking on this button you will get exe of Pycharm   just install it into your system. If you are facing problem to install then watch the above video i have explained step by step.

Step 2: Create Project : Open Pycharm and you will get a screen like below if you opening it first time.

Text to Speech in Python
Here click on create new project you will get a screen like below

Text to Speech in Python

Here first you need to select directory of your project and then type a name for your project after directory name and at last click the create button to create this project. After doing this your project will look like below screenshot.

Text to Speech in Python

Step 3:Create Python file: Now its time to create a python file for coding. Right click on project name select new and then select Python file
Text to Speech in Python

After clicking on Python file you will get a popup screen like below

Text to Speech in Python

Here in the above screen you just need to type a  name for python file in my case it is program but it is not compulsory , you can type any name for python file. After doing this you will get a screen  like below

Text to Speech in Python


Step 4:Store Textfile into project :Now create any text file and type some sentence into it and store this file into your project folder, after doing this your project screen will look like below
Text to Speech in Python

Here the name of text file is myfile.txt

Step 5: Install library (pyttsx3) : Before doing code for this we have to first, install the library which is used to convert text into speech. To install this library click on terminal which will be in left bottom of screen.

Text to Speech in Python

After click, a console screen will open in which you have to type pip install pyttsx3 and press enter after typing. For better understanding see the below screenshot.

Text to Speech in Python

Now wait for some second you will get a message like installed successfully. If you will get an error then watch the above video i have explained the solution.

Step 6: Write Python code:  Now i am going to explain the code line by line

Line 1: import library

import pyttsx3

This is the first line which used to import the library pyttsx3. Here pyttsx3 stands for
py=Python
t=Text
t=To
s=Speech
x3=Version3
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
read=file.read()
Here read() is a predefined function which is used to read a file.


Line 4: Initialize Speaker
speaker=pyttsx3.init()
Here speaker is a variable, pyttsx3 is module and init() is a predefined function that initializes the speaker.

Line 5: Pass file content into say
speaker.say(read)
Here say() is a predefined function of module pyttsx3 that will convert the text into speech.


Line 6: Run Speaker
speaker.runAndWait()
here runAndWait is a predefined function which is used to run the speech.

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

#import library
import  pyttsx3
#open file
file=open("myfile.txt")
#reading file
read=file.read()
#initializing speaker
speaker=pyttsx3.init()
#speech
speaker.say(read)
#runAndWait
speaker.runAndWait()

Here just copy this code and paste it into program.py file.

Step 7: Run Code: Now its time to execute this code just right click on the coding section and click on Run Program

Text to Speech in Python

When you will run this code then you will listen audio of the content of your file.

I hope now you can create  "Text to Speech 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