| pyttsx3 is a Python library that allows you to convert text into speech. It is a simple and easy-to-use library, making it ideal for projects requiring speech synthesis. Pyttsx3 works by leveraging various speech engines available on your system, such as the built-in Windows speech engine or the open-source eSpeak engine. This makes it a versatile tool for developers who need to add speech capabilities to their Python applications.
Here's a simple example to use pyttsx3 to say "Hi" in Python: First, install pyttsx3 if you haven’t: pip install pyttsx3
Then, use this code: import pyttsx3
# Initialize the TTS engine
engine = pyttsx3.init()
# Set the text you want to say
text = "Hi"
# Use the engine to say the text
engine.say(text)
# Run and wait for the speech to complete
engine.runAndWait()
This code will make your system say "Hi" using one of the available voices on your system. Below is the advanced version of sample with pyttsx3 to select voice: import pyttsx3
engine = pyttsx3.init()
# List available voices
voices = engine.getProperty('voices')
# Select a male or female voice if available
for voice in voices:
if "female" in voice.name.lower(): # Change to "male" for a male voice
engine.setProperty('voice', voice.id)
break
text = "Hello, this is a sample text to speech with a selected voice."
engine.say(text)
engine.runAndWait()
Below case is an example to generate audio file in mp3: import pyttsx3
# Initialize the pyttsx3 engine
engine = pyttsx3.init()
# Configure the voice properties (optional)
engine.setProperty('rate', 150) # Speed of speech
engine.setProperty('volume', 1) # Volume level (0.0 to 1.0)
# Choose a voice (optional)
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id) # Use the first available voice
# Text to convert to speech
text = "This is a sample text to be saved as an audio file using pyttsx3."
# Save the speech to an audio file
engine.save_to_file(text, 'output_audio.mp3')
engine.runAndWait()
print("Audio file created successfully as 'output_audio.mp3'")
Explanation engine.setProperty('rate', 150): Controls the speed of speech.engine.setProperty('volume', 1): Sets the volume (1.0 is max).engine.setProperty('voice', voices[0].id): Sets the voice (you can explore different available voices).save_to_file(): Converts the text to speech and saves it as 'output_audio.mp3'.
This code will create an MP3 file called output_audio.mp3 containing the speech. pyttsx3 uses platform-specific speech synthesis engines (like SAPI5 on Windows and NSSpeechSynthesizer on macOS), so audio quality and available voices might vary depending on the operating system. pyttsx3 is a cross-platform library and works well on Linux distributions including CentOS. It uses the system's built-in text-to-speech engine, so you won't need to install any additional dependencies. Simply install the `pyttsx3` package using `pip` and you're ready to start using it.
You may also have interest to have the available voice list. import pyttsx3
# Initialize the pyttsx3 engine
engine = pyttsx3.init()
# Get the list of available voices
voices = engine.getProperty('voices')
# Print the available voices with their details
for index, voice in enumerate(voices):
print(f"Voice {index + 1}:")
print(f" - ID: {voice.id}")
print(f" - Name: {voice.name}")
print(f" - Languages: {voice.languages}") # Languages (if available)
print(f" - Gender: {voice.gender}") # Gender (if available)
print(f" - Age: {voice.age}") # Age (if available)
print("\n")
# Optionally, you can also print the number of available voices
print(f"Total voices available: {len(voices)}")
Explanation voice.id: A unique identifier for the voice, often used to set the voice.voice.name: The name of the voice (this can vary by platform).voice.languages: Languages supported by the voice (if the information is available).voice.gender and voice.age: Gender and age attributes (availability depends on the platform and engine).
This code will print out all available voices, including any language information. However, pyttsx3 may not always provide specific language codes or detailed attributes depending on the operating system’s TTS engine. |