Skip to main content


Text to speech using Python

See the video first for demo:-

First create a text file named :- text_speech.txt

And type what ever you want it to read and save it.

Works offline:-

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#Text to speech (offline)
#python -m pip install pywin32
#Program description:-
#Directly read from text without saving any mp3 file

import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
file = open("text_speech.txt","r")
#print(file.read())
string = file.read()
speaker.Speak(string)

If error check and install using pip (python -m pip install pywin32)

Works online:-

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#Text to speech (Online)
# pip install gTTS
#Program description:-
# It uses google translate in order to create
# mp3 file from text

from gtts import gTTS as gt
import os

file = open("text_speech.txt","r")
#print(file.read())

def read(text):
       sound_file="file.mp3"
       a = gt(text,"en")
       a.save(sound_file)
       os.system('mpg123 ' + sound_file)

try:
       print("Generating...")
       read(file.read())
except:
       print("Enter Proper Text")
       print("No connection to Google Translate")
finally:
       print("Done")
If error check and install using pip (pip install gTTS)


🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍

Popular Posts

Python underline string, Python underline text, Underline python print

Python pip - Installing modules from IDLE (Pyton GUI) for python 3.7

Top 40 Python - String Processing in Python | Working with String in Python

Top 11 Essential Python Tips and Tricks

Python Program - When was I born? / Date of Birth / MY BIRTHDAY (using Python3+)





Subscribe to our Channel


Follow us on Facebook Page

Join our python facebook groups



Join us on Telegram