How to Change Voice in pyttsx3 in Python

share link

by Abdul Rawoof A R dot icon Updated: Feb 3, 2023

technology logo
technology logo

Solution Kit Solution Kit  

To convert 'text-to-speech' we're using the pyttsx3 library in this kit. We can also change the voice of the computer to another voice. This module provides two different types of voices one is male and another one is female.


In our pyttsx3-dependent library, we're getting the voices by running a python file. The 'voiceChange()' function is invoked and an instance is initialized of the module. In this voice library, using the 'setProperty()' method and we get the available voices and we can change the user's voice ID accordingly to bring a male or female voice. If we set the speed rate of pyttsx3 below 200, the voice will speak slowly and if we set it above 200, it will increase the rate of the engine.


To change the voice of the pyttsx3 computer,

  • we'll have to get the list of objects of voices.
  • In the pyttsx3 package, we used 'getProperty()' to take a string as a parameter and return an object matching.


There are some APIs available to convert text to speech in python. Python Text to Speech API commonly known as the pyttsx3 API is one of them. pyttsx3 is a very easy-to-use tool that is used to convert the text entered into audio. We can also change the default language to your favorite language. For that, you should need to install additional languages to change the default language of your choice. After installing the languages, you can get the list of available voices from voices properties on your computer and then you can choose your language from the list.


Here is an example of how you can change the voice in pyttsx3 in Python,

Fig : Preview of the output that you will get on running this code from your IDE.

Code

In this solution we're using PyAudio and pyttsx3 libraries.

Instructions

Follow the steps carefully to get the output easily.

  1. Install PyAudio and pyttsx3 on your IDE(Any of your favorite IDE).
  2. Open terminal and install the above mentioned libraries using the command given in 3 & 4 steps.
  3. PyAudio - pip install PyAudio.
  4. pyttsx3 - pip install pyttsx3.
  5. Copy the snippet using the 'copy' and paste it in your IDE.
  6. Run the file to generate the output.


I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.


I found this code snippet by searching for 'How to change the voice in pyttsx3' in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in PyCharm 2021.3.
  2. The solution is tested on Python 3.9.7.
  3. PyAudio version-0.2.13.
  4. pyttsx3version-2.90.


Using this solution, we are able to change the voice in pyttsx3 with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to change the voice in pyttsx3.

Dependent Libraries

pyaudioby sschaetz

Python doticonstar image 0 doticonVersion:Currentdoticon
License: Permissive (MIT)

PyAudio Fork

Support
    Quality
      Security
        License
          Reuse

            pyaudioby sschaetz

            Python doticon star image 0 doticonVersion:Currentdoticon License: Permissive (MIT)

            PyAudio Fork
            Support
              Quality
                Security
                  License
                    Reuse

                      pyttsx3by nateshmbhat

                      Python doticonstar image 1571 doticonVersion:v.2.90doticon
                      License: Weak Copyleft (MPL-2.0)

                      Offline Text To Speech synthesis for python

                      Support
                        Quality
                          Security
                            License
                              Reuse

                                pyttsx3by nateshmbhat

                                Python doticon star image 1571 doticonVersion:v.2.90doticon License: Weak Copyleft (MPL-2.0)

                                Offline Text To Speech synthesis for python
                                Support
                                  Quality
                                    Security
                                      License
                                        Reuse

                                          You can also search for any dependent libraries on kandi like 'PyAudio' and 'pyttsx3'.

                                          Support

                                          1. For any support on kandi solution kits, please use the chat
                                          2. For further learning resources, visit the Open Weaver Community learning page.


                                          See similar Kits and Libraries