Skip to main content

Python Text to Speech Example

Here you will get python text to speech example.

As we know, some people have difficulty reading large amounts of text due to dyslexia and other learning disabilities. Some people have basic literary levels. They often get frustrated trying to browse the internet because so much of it is in text form or on other hand some people prefer to listen or watch a news article (or something like this) instead of reading. So to solve all these problems a concept comes into mind that is ”text to speech”.

So in this tutorial we are going to learn that how to convert text to speech in Python. Here we’ll show you two best and easiest ways to convert your text into speech

  1. Text to speech without internet connection (using pyttsx3)
  2. Text to speech having internet connection (using gTTS)

Python Text to Speech Example

Method 1: Using pyttsx3

Pyttsx3 is an offline cross-platform Test-to-Speech library which is compatible with both Python 3 and Python 2 and supports multiple TTS engines

To use pyttsx3, first we have to download and install it. In order to install it open your command prompt or terminal and type this command.

pip install pyttsx3

If you’re using windows operating system then you also have to install “pypiwin32” to make it work. To install pypiwin32 again type this command and hit enter in command prompt.

python  -m  pip install pypiwin32

Make sure you’ve internet connection while running both of the command. It is one time process, after you’ve installed pyttsx3 now to use it, the program will be as shown below.

import pyttsx3
engine = pyttsx3.init()
engine.say("hello crazy programmer")
engine.setProperty('rate',120)
engine.setProperty('volume', 0.9)
engine.runAndWait()

In this program, in first Line we’re initializing pyttsx3 for use then we’re passing the text in method say(). After it we’re setting some properties like volume and rate of the voice. Here we’re passing 120 as rate, which means it will speak 120 words per minute and last line of above program will be produce an audio saying “hello crazy  programmer”.

We can also modify the voice like we can change it into female voice (by default its male), age and language. For more information please visit http://pyttsx3.readthedocs.io/en/latest/engine.html

Method 2: Using gTTS (Google Text to Speech)

Google Text to Speech is one of the best TTS API out there, because it will generate audio as approximately similar to human voice while other APIs generate audio like a metallic voice or robotic voice. But there is also a disadvantage of gTTS, it will need an internet connection to convert the text into an audio. So it can be slow then other offline APIs.

To install gTTS API open your command prompt or terminal and type this command:

pip  install gTTS

Program for conversion will be as shown below.

from gtts import gTTS
tts = gTTS(text="Hello crazy programmer", lang='en')
tts.save("audio.mp3")

Unlike other APIs it will generate an audio and will save into the same directory where your program stored.

To play this audio we’ll need another tool to play audio on command line.

If you’re using Linux (eg. Ubuntu) then mpg321 will be best command line player.

To install it open terminal and type this command-

sudo apt-get install mpg321

Now we can use this command to play any audio on command line:

mpg321 audio.mp3 -quiet

To run this command in python program, add these two lines into above program

import os     #will be on the top
os.system('mpg321 audio.mp3 -quiet')

On other hand, for windows, we doesn’t have to install any new software or API to play the mp3 file. All we have to do is open command prompt and enter the name of your file it will play that file using your default media player. So to run this command in python add these two lines in above program.

import os       #will be on the top
os.system("audio.mp3")

For more information on gTTS please visit https://pypi.org/project/gTTS/

Comment below if you have queries regarding python text to speech conversion.

The post Python Text to Speech Example appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2018/05/python-text-to-speech.html

Comments

Popular posts from this blog

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan...

15 Web Design Trends to Watch in 2018

The modern world is full of extraordinary things that influence our imagination and mood. Our soul needs a perfect atmosphere and impressive spots. To apply such things in practice, we have submitted the list of the web trends that deserve your attention. Robert frost design analysis will meet all your wishes and expectations. Image Source Web Design Trends to Watch in 2018 1. More Organic Shapes Until this year, web design, as well as mobile design, were based on the right-angled and sharp-edged shapes. However, it seems that this year will bring some significant changes in the field of web design. The recent trends will offer the absolute rounded corners. In addition, the web design of 2018 will make the real things look like the cartoonish ones. 2.   Bold Minimalism Although some of you may think that this web design trend will not attract the Internet users. Indeed, the notion of minimalism is often associated with boredom and dullness. However, in this case, bold ...

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and...