There are several APIs available to convert text to speech in python. One such APIs is the Python Text to Speech API commonly known as the pyttsx3 API. pyttsx3 is a very easy to use tool which converts the text entered, into audio.
Installation
To install the pyttsx3 API, open terminal and write
pip install pyttsx3
This library is dependent on win32 for which we may get an error while executing the program. To avoid that simply install pypiwin32 in your environment.
pip install pypiwin32
Some of the important functions available in pyttsx3 are:
- pyttsx3.init([driverName : string, debug : bool]) – Gets a reference to an engine instance that will use the given driver. If the requested driver is already in use by another engine instance, that engine is returned. Otherwise, a new engine is created.
- getProperty(name : string) – Gets the current value of an engine property.
- setProperty(name, value) – Queues a command to set an engine property. The new property value affects all utterances queued after this command.
- say(text : unicode, name : string) – Queues a command to speak an utterance. The speech is output according to the properties set before this command in the queue.
- runAndWait() – Blocks while processing all currently queued commands. Invokes callbacks for engine notifications appropriately. Returns when all commands queued before this call are emptied from the queue.
Now we are all set to write a sample program that converts text to speech.
# Python program to show # how to convert text to speech import pyttsx3 # Initialize the converter converter = pyttsx3.init() # Set properties before adding # Things to say # Sets speed percent # Can be more than 100 converter.setProperty('rate', 150) # Set volume 0-1 converter.setProperty('volume', 0.7) # Queue the entered text # There will be a pause between # each one like a pause in # a sentence converter.say("Hello GeeksforGeeks") converter.say("I'm also a geek") # Empties the say() queue # Program will not continue # until all speech is done talking converter.runAndWait() |
Output:
The output of the above program will be a voice saying, “Hello GeeksforGeeks” and “I’m also a geek”.
Changing Voice
Suppose, you want to change the voice generated from male to female. How do you go about it? Let us see.
As you will notice, when you run the above code to bring about the text to speech conversion, the voice that responds is a male voice. To change the voice you can get the list of available voices by getting voices properties from the engine and you can change the voice according to the voice available in your system.
To get the list of voices, write the following code.
voices = converter.getProperty('voices') for voice in voices: # to get the info. about various voices in our PC print("Voice:") print("ID: %s" %voice.id) print("Name: %s" %voice.name) print("Age: %s" %voice.age) print("Gender: %s" %voice.gender) print("Languages Known: %s" %voice.languages) |
Output:

To change the voice, set the voice using setProperty() method. Voice Id found above is used to set the voice.
Below is the implementation of changing voice.
voice_id = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0" # Use female voice converter.setProperty('voice', voice_id) converter.runAndWait() |
Now you’ll be able to switch between voices as and when you want. You can try out running a for loop to assign different statements to different voices. Run the code and enjoy the result.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Personal Voice Assistant in Python
- Voice search Wikipedia using Python
- Create a Voice Recorder using Python
- Build a Voice Recorder GUI using Python
- Python Script to Shutdown your PC using Voice Commands
- Voice Assistant using python
- Build an Application for Changing PC's Wallpaper using Python
- Changing Class Members in Python
- PyQt5 - Changing border of Progress Bar
- PyQt5 - Adding/Changing caption of Check Box
- PyQt5 - Changing size of Indicator in Check Box
- PyQt5 - Changing color of pressed Push Button
- PyQt5 - Changing background color of Label when hover
- PyQt5 - Changing background color of Push Button when mouse hover over it
- PyQt5 QSpinBox – Changing value using stepBy method
- PyQt5 QSpinBox - Setting/Changing Geometry
- PyQt5 QCalendarWidget - Changing Cursor Shape
- PyQt5 QCalendarWidget - Changing Geometry
- PyQt5 QColorDialog - Changing Font
- PyQt5 QCommandLinkButton - Changing Icon
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

