Do you want to convert your text to speech just by using less than 10 lines of code? Well! yes, using Python’s pyttsx3 module (Python text to speech module), you can convert any text to speech. The most important part of this library is- it works offline and is compatible with both Python 2 and 3. It is a very easy-to-use tool that converts the entire text into speech. In this article, we will use real Jarvis Voice to create an audiobook in Python for free.
It supports voices available in your system. For example, if you have three voices installed in your system, you can use any of them in your code.
The module “pyttsx3” supports three TTS engines:
- sapi5: Sapi5 on Windows
- nsss: NSSpeechSynthesizer on Mac OS X
- espeak: eSpeak on every other platform
How To Install Python Text To Speech Module?
To install the Python text to speech module, install pyttxs3 from pypi.org using the code below.
pip install pyttsx3
How To Implement pyttsx3 Module?
To implement the pyttsx3 module, use the below self-explanatory code.
# Import the required module for text # to speech conversion import pyttsx3 # init function to get an engine instance for the speech synthesis engine = pyttsx3.init() # say method on the engine that passing input text to be spoken engine.say('Good Morning Sir, how may I help you?') # run and wait method, it processes the voice commands. engine.runAndWait()
In the above code block, engine.say() is used to convert the provided text into speech.
Change Rate – Python Text To Speech
To increase or decrease the speech rate, you can use the following code.
# change the speech rate engine.setProperty('rate',250)
Change Audiobook Voice
To change the voice of the audiobook in Python text to speech, use the below code to implement.
# choose a voice based on the voice id engine.setProperty('voice',voices.id) # Selected Brian Voice (third party)
Code With A Sample Jarvis Dialogue From Iron Man Movie – A Complete Implementation Of Python Text To Speech
## Import the required module for text # to speech conversion import pyttsx3 # init function to get an engine instance for the speech synthesis engine = pyttsx3.init() # change the speech rate engine.setProperty('rate',250) # get the available voices voices = engine.getProperty('voices') # choose a voice based on the voice id engine.setProperty('voice',voices.id) # Selected Brian Voice # say method on the engine that passing input text to be spoken engine.say('''Good Morning sir, This is Jarvis. It's 7 A.M.. The weather in Malibu is 72 degrees with scattered clouds. The surf conditions are fair with waist to shoulder highlines, high tide will be at 10:52 a.m.. This sample audio is created by Pykit.org.''') # run and wait method, it processes the voice commands. engine.runAndWait()
Live Example Of Above Code
In this article, we discussed the Python pyttsx3 module with a live example. We used the pyttsx3 module to create a text-to-speech converter in Python and also implemented using a real Jarvis voice. If you are interested in learning more about Python, read my articles on Chatterbot in Python.
Feel free to contact us in case of any queries.