Here you’ll get python text to speech example.
As we know, some folks have a problem reading massive amounts of text because of a learning disorder and different learning disabilities. Some folks have basic literacy levels.
They typically get pissed off attempting to browse the net as a result of such a lot of it’s in text type or on different handsome folks choose to listen or watch a news story (or one thing like this) rather than reading.
thus to unravel of these issues an inspiration comes into mind that’s ”text to speech”.
So during this tutorial, we tend to are aiming to learn that the way to convert text to speech in Python.
Here we’ll show you 2 best and best ways that to convert your text into speech
Text to speech while not net affiliation (using pyttsx3)
Text to speech having a net affiliation (using gTTS)
Python Text to Speech Example
Method 1: victimization pyttsx3
Pyttsx3 is associate degree offline cross-platform Test-to-Speech library that is compatible with each Python three and Python a pair of and supports multiple TTS engines
To use pyttsx3, 1st we’ve to transfer and install it. so as to put in it open your prompt or terminal and kind this command.
pip install pyttsx3
If you’re victimization windows software system then you furthermore may install “pypiwin32” to form it works. to put in pypiwin32 once more sort this command and hit enter in prompt.
python -m pip install pypiwin32
Make sure you’ve net affiliation whereas running each of the command. it’s just once the method, when you’ve put in pyttsx3 currently to use it, the program is as shown below.
import pyttsx3
engine = pyttsx3.init()
engine.say("hello Tech")
engine.setProperty('rate',120)
engine.setProperty('volume', 0.9)
engine.runAndWait()
Method 2: victimization gTTS (Google Text to Speech)
Google Text to Speech is one in all the most effective TTS API out there, as a result of it’ll generate audio as about just like human voice whereas the different genus
Apis generate audio sort of an aluminiferous voice or robotic voice. however there’s additionally a drawback of gTTS, it’ll want a web affiliation to convert the text into associate degree audio. thus it is slow then different offline genus Apis.
To install gTTS API open your prompt or terminal and kind this command:
pip install gTTS
Program for the conversion is as shown below.
from gtts import gTTS
tts = gTTS(text="Hello Tech", lang='en')
tts.save("audio.mp3")
Unlike different genus Apis, it’ll generate associate degree audio and can save into a similar directory wherever your program holds on.
To play this audio we’ll want another tool to play audio on instruction.
If you’re victimization UNIX (eg. Ubuntu) then mpg321 is best instruction player.
To install it open terminal and write this command-
sudo apt-get install mpg321
Now we are able to use this command to play any audio on the command line:
mpg321 audio.mp3 -quiet
To run this command in a python program, add these 2 lines into on top of program
import os #will be on the top
os.system('mpg321 audio.mp3 -quiet')
On the different hand, for windows, we tend to oughtn’t to install any new software system or API to play the mp3 file.
All we’ve to try to do is open prompt and enter the name of your file it’ll play that file victimization your default media player.
thus to run this command in python add these 2 lines in on top of the program.
import os #will be on the top
os.system("audio.mp3")
Comment below if you’ve got queries relating to python text to speech conversion.