How to Convert Text to Speech in Python ? Make Your Own API

Convert Text to Speech in Python

Converting text to speech for your project must be an interesting task. There are many APIs available in the market that provides this functionality. Some of them you already know Text to Voice by Amazon AWS, speech recognition by Google and many more. But if you want to build your own then you have come to the right place. In this entire post, you will know how to convert text to speech in Python step by step.

Step by Step Guide to Convert Text to Speech

Step 1: Import or Install the necessary libraries.

Here I am importing all the necessary libraries that are required for this project. One is pyttsx3 and the other is pywin32(You have to install it to use pyttsx3).

import pyttsx3

Step 2:  Call the engine factory

Initialize the engine factory that will be used for getting the text and converting to Speech

engine = pyttsx3.init()

Step 3: Use the text

In this step, let’s say you want to convert text to speech. Write there and run the program you will listen to the speech . Use the following code.

engine.say("Data Science Learner")
engine.runAndWait()

Here you can see I am passing the text inside the engine.say() method and engine.runAndWait() that takes all the text words inside the queue and run until it is empty.

That’s all the thing required for converting text to speech. In the below section you will know what are the other things you can do using this Python Module.

Others Things you can do with Pyttsx3

1. Change the Voice

You can also change or set the voice. Use the following code.

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
engine.say('Data Science Learner')
engine.runAndWait()

Here I am first getting all the voices using the getProperty() method and then set the engine to use the voice[0].id. Currently, there are only two voices, therefore, you must use 0 and 1. Run it you will see the differences

2. Speech Rate

You can also change the rate of speech using the Python module. You have to just call the getProperty(“rate”) and setProperty() for passing the speed.

rate. engine = pyttsx3.init()
rate = engine.getProperty('rate')
engine.setProperty('rate', rate+50)
engine.say('Data Science Learner')
engine.runAndWait()

3. Changing the Volume

For changing the volume of the voice you again have to call the getProperty() and setProperty() and add or subtract some floating values from the existing volumes. Use the following code.

engine = pyttsx3.init()
volume = engine.getProperty('volume')
engine.setProperty('volume', volume-0.50)
engine.say('Data Science Learner')
engine.runAndWait()

This way you can play with the text to a speech Python module. I hope you have liked Convert Text to Speech in Python tutorial. You can also build your own text to speech API using it. To do so you have to Make a Flask Application. If you have any queries regarding this then please contact us for more information. You can also read the official Module Documentation.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner