Example 1: Using pyttsx3
Now that we have installed the pyttsx3 library, let's see how we can use it to convert text to speech.
Let’s look at an example :
import pyttsx3
# Initialize the pyttsx3 engine
engine = pyttsx3.init()
# Set the text to be converted to speech
text = "Hello, this is an example of text-to-speech using pyttsx3 in Python."
# Use the engine to convert the text to speech
engine.say(text)
# Run the engine & wait for the speech to finish
engine.runAndWait()
In this code :
1. We import the pyttsx3 library.
2. We initialize the pyttsx3 engine using `pyttsx3.init()`.
3. We set the text that we want to convert to speech & assign it to the `text` variable.
4. We use the `say()` method of the engine to pass the text to be converted.
5. Finally, we call the `runAndWait()` method to start the speech synthesis & wait for it to complete.
When you run this code, you will hear the text being spoken out loud by the default voice of your system.
pyttsx3 offers additional customization options, such as changing the voice, adjusting the speech rate, & modifying the volume.
For example :
import pyttsx3
engine = pyttsx3.init()
# Get the available voices
voices = engine.getProperty('voices')
# Set the desired voice (index 1 represents the second voice in the list)
engine.setProperty('voice', voices[1].id)
# Set the speech rate (words per minute)
engine.setProperty('rate', 150)
# Set the volume (0.0 to 1.0)
engine.setProperty('volume', 0.7)
text = "This is an example of customizing the speech using pyttsx3."
engine.say(text)
engine.runAndWait()
In this example, we retrieve the available voices using `engine.getProperty('voices')`. We then set the desired voice using `engine.setProperty('voice', voices[1].id)`. You can experiment with different voice indexes to find the one that suits your needs.
We also set the speech rate using `engine.setProperty('rate', 150)`, where 150 represents the number of words spoken per minute. Adjust this value to make the speech faster or slower.
Finally, we set the volume using `engine.setProperty('volume', 0.7)`, where 0.7 represents 70% of the maximum volume. You can choose a value between 0.0 & 1.0 to control the loudness of the speech.
Example 2: Using gTTS
In this example, we will explore how to use the gTTS library to convert text to speech & save the output as an audio file. Here's how you can do it:
from gtts import gTTS
import os
# Set the text to be converted to speech
text = "This is an example of text-to-speech using gTTS in Python."
# Create a gTTS object with the text & language (en for English)
tts = gTTS(text=text, lang='en')
# Save the speech as an audio file
tts.save("output.mp3")
# Play the audio file
os.system("start output.mp3")
In this code :
1. We import the gTTS library & the os module.
2. We set the text that we want to convert to speech & assign it to the `text` variable.
3. We create a gTTS object using `gTTS(text=text, lang='en')`, specifying the text & the language (in this case, 'en' for English).
4. We use the `save()` method of the gTTS object to save the speech as an audio file named "output.mp3".
5. Finally, we use `os.system("start output.mp3")` to play the audio file.
When you run this code, it will generate an audio file named "output.mp3" in the same directory as your Python script. The audio file will contain the speech generated from the provided text.
Note: gTTS supports multiple languages, & you can specify the desired language using the `lang` parameter. For example, to generate speech in Hindi, you can use `lang='hi'`.
Frequently Asked Questions
Can I use pyttsx3 or gTTS for commercial projects?
Yes, both pyttsx3 & gTTS are open-source libraries & can be used freely in commercial projects. However, it's always a good idea to review the specific license terms of each library before using them in your projects.
How can I change the voice or accent of the speech output?
With pyttsx3, you can change the voice by selecting a different voice from the available voices using engine.setProperty('voice', voices[index].id). gTTS, on the other hand, uses Google's TTS API, which provides a limited set of voices & accents based on the specified language.
Is an internet connection required to use pyttsx3 or gTTS?
pyttsx3 works offline & does not require an internet connection, as it uses the text-to-speech engines installed on your system. However, gTTS relies on Google's TTS API, which requires an active internet connection to generate speech.
Conclusion
In this article, we learned how to convert text to speech using Python. We discussed two popular libraries, pyttsx3 & gTTS, and showed how to use them to generate spoken output from written text. pyttsx3 offers offline functionality & customization options, while gTTS utilizes Google's TTS API & supports multiple languages.