Converting text to speech (TTS) in Python can be easily achieved using several libraries. One of the most popular and straightforward libraries is gTTS (Google Text-to-Speech). Another widely used option is pyttsx3, which works offline and supports multiple TTS engines.
Below is a detailed guide on how to convert text to speech in Python using these libraries:
Using gTTS (Google Text-to-Speech)
gTTS is a Python library and CLI tool to interface with Google Translate’s text-to-speech API. It requires an internet connection.
Installation:
Example code:
Using pyttsx3 (Offline Text-to-Speech)
pyttsx3 works offline and uses the speech engines already installed on your system (SAPI5 on Windows, NSSpeechSynthesizer on macOS, espeak on Linux).
Installation:
Example code:
Additional Features & Customization
-
Changing voice (male/female):
-
Saving speech to file (using pyttsx3):
Summary
-
Use gTTS for quick, easy, and natural-sounding speech with internet access.
-
Use pyttsx3 for offline usage with control over voice properties and engine options.
This approach enables you to integrate text-to-speech functionality smoothly into your Python applications or scripts.