Back to Tutorials
Python

Voice Assistant in Python

Build a voice assistant that listens to your voice, understands commands, and responds by speaking - open websites, tell the time, and more.

~40 mins Intermediate Python 3 Speech Recognition
Skills & Concepts You'll Gain
Tech Used: Python 3, SpeechRecognition library, pyttsx3 library, datetime module, webbrowser module
Requirements
  • Python 3 installed
  • A working microphone
  • Internet connection (for Google Speech API)
  • VS Code or any editor
Libraries to Install
  • SpeechRecognition
  • pyttsx3
  • pyaudio
Step 1

Install Required Libraries

Open your terminal (Command Prompt on Windows) and run these commands one by one:

Terminal / CMD
pip install SpeechRecognition pyttsx3 pyaudio
If pyaudio fails to install on Windows, run these two commands instead:

pip install pipwin
pipwin install pyaudio

After installing, create a new file called voice_assistant.py in a folder of your choice.

Step 2

Understand the Libraries

Before writing code, understand what each library does:

Step 3

Write the Code

Copy this complete code into your voice_assistant.py file:

voice_assistant.py
# Voice Assistant - NKDevSpace Tutorial

import speech_recognition as sr
import pyttsx3
import datetime
import webbrowser

# --- Initialize the text-to-speech engine ---
engine = pyttsx3.init()
engine.setProperty('rate', 160)   # Speed of speech (words per minute)
engine.setProperty('volume', 1.0) # Volume: 0.0 to 1.0

def speak(text):
    """Make the assistant speak out loud."""
    print(f"Assistant: {text}")
    engine.say(text)
    engine.runAndWait()

def take_command():
    """Listen to the microphone and return the spoken text."""
    recognizer = sr.Recognizer()

    with sr.Microphone() as source:
        print("\nListening... (speak now)")
        recognizer.pause_threshold = 1  # Wait 1 second of silence before stopping
        audio = recognizer.listen(source)

    try:
        print("Recognizing...")
        command = recognizer.recognize_google(audio)
        print(f"You said: {command}")
        return command.lower()
    except sr.UnknownValueError:
        speak("Sorry, I could not understand. Please say that again.")
        return ""
    except sr.RequestError:
        speak("Sorry, I am having trouble connecting. Check your internet.")
        return ""

def run_assistant():
    """Process the voice command and perform the right action."""
    command = take_command()

    if not command:
        return

    if "time" in command:
        current_time = datetime.datetime.now().strftime("%I:%M %p")
        speak(f"The current time is {current_time}")

    elif "date" in command:
        today = datetime.datetime.now().strftime("%B %d, %Y")
        speak(f"Today's date is {today}")

    elif "open google" in command:
        speak("Opening Google")
        webbrowser.open("https://www.google.com")

    elif "open youtube" in command:
        speak("Opening YouTube")
        webbrowser.open("https://www.youtube.com")

    elif "open github" in command:
        speak("Opening GitHub")
        webbrowser.open("https://www.github.com")

    elif "hello" in command or "hi" in command:
        speak("Hello! I am your voice assistant. How can I help you?")

    elif "your name" in command:
        speak("I am NKBot, your personal voice assistant built with Python.")

    elif "stop" in command or "exit" in command or "bye" in command:
        speak("Goodbye! Have a great day.")
        exit()

    else:
        speak(f"I heard you say: {command}. I don't know how to do that yet.")

# ── Main loop ──
speak("Voice Assistant is ready. Say a command.")

while True:
    run_assistant()
Step 4

Code Explanation

engine = pyttsx3.init()
Creates the text-to-speech engine. setProperty('rate', 160) controls how fast it speaks - lower number = slower speech. You can adjust this to your preference.
engine.say(text) → engine.runAndWait()
say() queues the text to be spoken. runAndWait() actually plays the speech and waits until it finishes before continuing the program. Always use both together.
recognizer.recognize_google(audio)
Sends the recorded audio to Google's free Speech-to-Text API over the internet and returns the recognized text. This is why you need an internet connection. The result is converted to lowercase with .lower() so commands like "Open Google" and "open google" both work.
except sr.UnknownValueError
This catches the error when Google couldn't understand what was said (too quiet, background noise, etc.). Instead of crashing, we politely ask the user to repeat.
if "time" in command
We use in to check if a keyword exists anywhere in the command. So "what is the time" and "tell me the time" both match because both contain the word "time".
while True: run_assistant()
The assistant keeps listening in an infinite loop. The only way to stop it is to say "stop", "exit", or "bye" - which calls exit() to end the program.
Step 5

Run the Program

  1. Open your terminal and navigate to your project folder:
    CMD / Terminal
    cd path\to\your\folder
    For example, if your file is on the Desktop:
    CMD
    cd C:\Users\YourName\Desktop
  2. Run the program:
    CMD / Terminal
    python voice_assistant.py
  3. You will hear "Voice Assistant is ready. Say a command."
  4. Try saying: "What is the time" - it should speak the current time
  5. Try: "Open YouTube" - your browser should open YouTube
  6. Say "bye" to stop the assistant
Speak clearly and at a normal pace. If it keeps saying "I could not understand", check that your microphone is set as the default input device in your system settings.
Step 6

Try It Yourself - Challenges

Previous Tutorial Next Tutorial