20 lines
474 B
Python
20 lines
474 B
Python
import pyttsx3
|
|
|
|
def text_to_speech(text):
|
|
# Initialize the TTS engine
|
|
engine = pyttsx3.init()
|
|
|
|
# Set properties (optional)
|
|
engine.setProperty('rate', 130) # Speed of speech
|
|
engine.setProperty('volume', 0.6) # Volume level (0.0 to 1.0)
|
|
|
|
# Convert text to speech
|
|
engine.say(text)
|
|
|
|
# Wait for the speech to finish
|
|
engine.runAndWait()
|
|
|
|
# Example usage
|
|
text = "Hello, this is a simple text-to-speech example in Python."
|
|
text_to_speech(text)
|