Untitled85
August 19, 2025
[5]: pip install pyttsx3
Requirement already satisfied: pyttsx3 in
c:\users\ajays\anaconda3\envs\tf_env\lib\site-packages (2.99)
Requirement already satisfied: comtypes in
c:\users\ajays\anaconda3\envs\tf_env\lib\site-packages (from pyttsx3) (1.4.11)
Requirement already satisfied: pypiwin32 in
c:\users\ajays\anaconda3\envs\tf_env\lib\site-packages (from pyttsx3) (223)
Requirement already satisfied: pywin32 in
c:\users\ajays\anaconda3\envs\tf_env\lib\site-packages (from pyttsx3) (308)
Note: you may need to restart the kernel to use updated packages.
[8]: import pyttsx3
# --------------------------
# Function: Convert text to speech directly
# --------------------------
def speak_text(text, rate=150, volume=1.0, voice_id=None):
try:
# Initialize the engine
engine = [Link]()
# Set speaking rate (default ~200 wpm)
[Link]('rate', rate)
# Set volume (0.0 to 1.0)
[Link]('volume', volume)
# Set voice (male/female depending on system)
voices = [Link]('voices')
if voice_id is not None and 0 <= voice_id < len(voices):
[Link]('voice', voices[voice_id].id)
# Speak the text
print("Speaking...")
[Link](text)
[Link]()
1
except Exception as e:
print("Error:", e)
[9]: # Main Program
# --------------------------
def main():
while True:
print("\n====== TEXT TO SPEECH (Direct Output) ======")
print("1. Enter text manually")
print("2. Exit")
choice = input("Enter your choice (1-2): ")
if choice == "1":
user_text = input("\nEnter the text you want to speak: ")
# Optional: let user select voice
print("\nChoose voice: 0=Default Male, 1=Default Female (may vary␣
↪by system)")
voice_choice = input("Enter choice (0/1): ").strip()
voice_id = int(voice_choice) if voice_choice.isdigit() else None
speak_text(user_text, rate=160, volume=1.0, voice_id=voice_id)
elif choice == "2":
print("Exiting program. Goodbye!")
break
else:
print("Invalid choice! Please enter 1 or 2.")
[10]: # Run program
if __name__ == "__main__":
main()
====== TEXT TO SPEECH (Direct Output) ======
1. Enter text manually
2. Exit
Enter your choice (1-2): Hello, VKB sir How are you
Invalid choice! Please enter 1 or 2.
====== TEXT TO SPEECH (Direct Output) ======
1. Enter text manually
2. Exit
Enter your choice (1-2): 1
Enter the text you want to speak: 1
2
Choose voice: 0=Default Male, 1=Default Female (may vary by system)
Enter choice (0/1): Hello, VKB sir How are you
� Speaking…
====== TEXT TO SPEECH (Direct Output) ======
1. Enter text manually
2. Exit
Enter your choice (1-2): 1
Enter the text you want to speak: Hello, VKB sir How are you
Choose voice: 0=Default Male, 1=Default Female (may vary by system)
Enter choice (0/1): 1
� Speaking…
====== TEXT TO SPEECH (Direct Output) ======
1. Enter text manually
2. Exit
Enter your choice (1-2): 2
Exiting program. Goodbye!
[ ]: