Hello friend today we are making our own personal assistant using python.
it was not much advance, this will totally depend on the if-elif-else
function which will we used in our program. You can make it fully
functionable as your wish. Follow below step to make it.
- First download all the modules which we use to create our personal assistant, for this open your command prompt in window and type the code given below.
- After the downloading is complete, open Python IDLE on your computer and import all the modules.
- Now we will set the voice of our assistant. 0 is for Male voice and 1 is for Female voice. You can choose any of them.
- Now we put a some greeting message like "Hello Sir". You can choose your own greeting message that assistant will wish you.
- Now we take input from user.
- Now we put a code that if user not give any command.
- :- Now we put a code to open application. You can choose any app from your computer.
- Now we put a code that if user want to shutdown their computer.
- Now we put a code that if user want to exit that application.
- Now we put a code that if user give any invalid command.
pip install pyttsx3
pip install os
#Module
import pyttsx3
import os
#voice_of_assistant
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
engine.runAndWait()
#greeting_message
print('Hello sir')
pyttsx3.speak('Hello sir')
print('I am jarvis, your personal assistant\n')
pyttsx3.speak('I am jarvis, your personal assistant')
#input_from_user
while True:
pyttsx3.speak('What can I do for you')
cmd = input('What can I do for you:- ')
#not_given_command
if cmd == '':
print("Sorry sir, can't recognize")
pyttsx3.speak("Sorry sir, can't recognize")
print('')
continue
#to_open_notepad
elif('notepad' in cmd):
print('Opening Notepad')
pyttsx3.speak('Opening Notepad')
os.system('notepad')
print('')
#to_open_paint
elif('paint' in cmd) or ('paints' in cmd) n cmd):
print('Opening Microsoft Paint')
pyttsx3.speak('Opening Microsoft Paint')
os.system('mspaint')
print('')
#shutdown_computer
elif('shutdown' in cmd) or ('restart' in cmd):
print('Ok sir shutting down your computer')
pyttsx3.speak('Ok sir shutting down your computer')
os.system("shutdown /s /t 1")
#to_close_jarvis
elif('exit' in cmd) or ('bye' in cmd):
print('Sure sir bye, Have a nice day')
pyttsx3.speak('Sure sir bye, Have a nice day')
break
#invalid_command
else :
print('invalid command')
pytsx3.speak('invalid command')
Code was completed and if you want add and more command then used Elif-function and to to open any other program you just have to put a line of code "os.startfile("address of file")". If you got any error put it on comment section. Let me know about that error.
Label:
Pycodes