-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
405 lines (293 loc) · 12.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import speech_recognition as sr
import pyttsx3
import datetime
import webbrowser
import wikipedia
import pyjokes
import docx
import os
from openpyxl import load_workbook
r = sr.Recognizer()
engine = pyttsx3.init()
engine.setProperty("rate", 150)
def voiceOutput(command):
engine.say(command)
engine.runAndWait()
def greetings():
hour = int(datetime.datetime.now().hour)
if 0 <= hour < 12:
print("Good Morning! ")
voiceOutput("Good Morning!")
elif 12 <= hour < 18:
print("Good Afternoon! ")
voiceOutput("Good Afternoon!")
else:
print("Good Evening! ")
voiceOutput("Good Evening!")
print("I am your virtual assistant. What can I do for you today?")
voiceOutput("I am your virtual Assistant. What can I do for you today?")
def greetingHello():
print("Hello. I am your virtual assistant. How can I assist you today?")
voiceOutput("Hello! I am your virtual Assistant. How can I assist you today?")
run_assistant()
def take_command():
try:
with sr.Microphone() as source1:
r.adjust_for_ambient_noise(source1, duration=0.2)
print('Getting input...')
voiceOutput("Getting input.")
audio1 = r.listen(source1)
print('Grabbing results...')
voiceOutput("Grabbing results")
MyText = r.recognize_google(audio1)
command = MyText.lower()
print("Your command : " + command)
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
except sr.UnknownValueError:
print("unknown error occurred")
return command
def tellDay():
day = datetime.datetime.today().weekday() + 1
Day_dict = {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday', 7: 'Sunday'}
if day in Day_dict.keys():
day_of_the_week = Day_dict[day]
print("The day is " + day_of_the_week + " today")
voiceOutput("Today is " + day_of_the_week)
def tellTime():
time = str(datetime.datetime.now())
print(time)
hour = time[11:13]
minutes = time[14:16]
voiceOutput("The time is " + hour + "Hours and" + minutes + "Minutes")
def searchWiki(command):
print("Processing input...")
voiceOutput("Processing input.")
command = command.replace("from ", "")
command = command.replace("wikipedia", "")
command = command.replace("search ", "")
command = command.replace("for ", "")
result = wikipedia.summary(command, sentences=4)
print("According to wikipedia...")
voiceOutput("According to wikipedia.")
print(result)
voiceOutput(result)
def nameTell(command):
command = command.replace("my name is ", "")
print("Hello " + command + ". How are you?")
voiceOutput("Hello " + command + ". How are you?")
def openClassroom():
print("Opening Google Classroom...")
voiceOutput("Opening Google Classroom.")
webbrowser.open("https://classroom.google.com")
def weather():
print("Getting weather forecast...")
voiceOutput("Getting weather forecast.")
webbrowser.open("https://www.google.com/search?q=weather+forecast")
def googleSearch(command):
command = command.replace("google", "")
command = command.replace("search ", "")
command = command.replace("for ", "")
command = command.replace("in ", "")
print("Searching Google for : " + command)
voiceOutput("Searching Google for " + command)
webbrowser.open("www.google.com/search?q=" + command)
def whoami():
print("You are the most wonderful programmer. You are Mr/Mrs Whatsoever. And you are my god because you created me!")
voiceOutput("You are the most wonderful programmer. You are Mister or Mistress Whatsoever. And you are my god because you created me!")
def tellAJoke():
joke = pyjokes.get_joke(language="en", category="neutral")
print("Here's a joke for you...")
voiceOutput("here is a joke for you.")
print(joke + " Ha ha ha!")
voiceOutput(joke + " Ha ha ha!")
def openMyComp():
print("Opening My Computer...")
voiceOutput("Opening My Computer.")
os.startfile("C:/Users/Siva/Desktop/Computer - Shortcut.lnk")
def openCalc():
print("Opening Calculator...")
voiceOutput("Opening calculator.")
os.startfile("C:/Windows/system32/calc.exe")
def openMSword():
print("Opening Microsoft Office Word...")
voiceOutput("Opening Microsoft Office Word.")
os.startfile("C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Microsoft Office/Microsoft Office Word 2007.lnk")
def openMSExcel():
print("Opening Microsoft Office Excel...")
voiceOutput("Opening Microsoft Office Excel.")
os.startfile("C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Microsoft Office/Microsoft Office Excel 2007.lnk")
def openMSppt():
print("Opening Microsoft Office PowerPoint...")
voiceOutput("Opening Microsoft Office Power point.")
os.startfile("C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Microsoft Office/Microsoft Office PowerPoint 2007.lnk")
def workWithExcel():
print("Name the excel file that you need to work with...")
voiceOutput("Name the excel file that you need to work with.")
command = take_command()
filename = command.lower()
workbook = load_workbook(filename="C:/Users/Siva/Desktop/" + filename + ".xlsx")
sheet = workbook.active
print("Opening " + filename + " worksheet...")
voiceOutput("Opening " + filename + " worksheet")
print("Would you like to display the records or edit a record?")
voiceOutput("Would you like to display the records or edit a record?")
command = take_command()
if 'display' in command:
print("Would you like to display all the records or display rows with particular word?")
voiceOutput("Would you like to display all the records or display rows with particular word?")
command = take_command()
if 'all the records' in command:
for value in sheet.iter_rows(min_row=1, max_row=11, min_col=1, max_col=4, values_only=True):
print(value)
elif 'particular' in command:
while(1):
print("Tell me the word to be searched...")
voiceOutput("Tell me the word to be searched")
command = take_command()
searchWord = command
found = 0
for value in sheet.iter_rows(min_row=1, max_row=11, min_col=1, max_col=4, values_only=True):
if searchWord in (str(value)).lower():
print(value)
found = 1
if found == 1:
break
else:
continue
elif 'edit' in command:
print("Do you wish to Enter new values or edit and replace an existing value?")
voiceOutput("Do you wish to Enter new values or edit and replace an existing value?")
command = take_command()
if 'replace' in command:
print("Tell the word to be found and replaced...")
voiceOutput("Tell the word to be found and replaced.")
command = take_command()
inputWord = command.lower()
found = 0
foundRow = 0
foundCol = 0
for row in range(1, sheet.max_row):
for col in range(1, sheet.max_column):
if inputWord == str(sheet.cell(row=row, column=col).value).lower():
found = 1
foundRow = row
foundCol = col
break
if found == 1:
print("Data found. Tell the word to be replaced in this position")
voiceOutput("Data found. Tell the word to be replaced in this position.")
command = take_command()
sheet.cell(row=foundRow, column=foundCol).value = command
else:
print("Data not found!")
voiceOutput("Data not found")
elif 'new value' in command:
print("Give column value to insert data...")
voiceOutput("Give column value to insert data.")
command = take_command()
col = ord(command) - 96
print("Give row value to insert data...")
voiceOutput("Give row value to insert data.")
command = take_command()
row = int(command)
print("Tell the data to be inserted in the cell ", chr(col + 96).upper(), row, "...")
voiceOutput("Tell the data to be inserted in the cell " + chr(col + 96) + "," + str(row))
command = take_command()
sheet.cell(row=row, column=col).value = command
print("Displaying changed Worksheet...")
voiceOutput("Displaying changed Worksheet.")
for value in sheet.iter_rows(min_row=1, max_row=sheet.max_row, min_col=1, max_col=sheet.max_column, values_only=True):
print(value)
workbook.save(filename="C:/Users/Siva/Desktop/" + filename + ".xlsx")
def wordProcess():
print('Insert text or Find a word in word file...')
voiceOutput('Insert text or Find a word in word file')
command = take_command()
if 'insert' in command:
doc = docx.Document()
print('Dictate the paragraph to be entered...')
voiceOutput('Dictate the paragraph to be entered')
command = take_command()
doc.add_paragraph(command)
print('Data successfully inserted...')
voiceOutput('Data successfully inserted')
doc.save('C:/Users/Siva/Desktop/Sample Document.docx')
if 'find' in command:
print("Tell the word to be found...")
voiceOutput("Tell the word to be found")
command = take_command()
checkWord = command.lower
doc = docx.Document('C:/Users/Siva/Desktop/Sample Document.docx')
paraList = []
found = 0
for para in doc.paragraphs:
paraList.append(para.text)
for para in paraList:
sen = para.split(" ")
for word in sen:
if word.lower() == checkWord:
found = 1
print('Word found in the paragraph...')
voiceOutput('Word found in the paragraph')
print(para)
if found == 0:
print('Word not found...')
voiceOutput('Word not found')
def run_assistant():
while 1:
command = take_command()
if 'what day' in command:
tellDay()
elif 'time' in command:
tellTime()
elif 'open google' in command:
print("Opening Google...")
voiceOutput("Opening Google.")
webbrowser.open("www.google.com")
elif 'open wikipedia' in command:
print("Opening Wikipedia...")
voiceOutput("Opening Wikipedia.")
webbrowser.open("www.wikipedia.org")
elif 'from wikipedia' in command:
searchWiki(command)
elif 'my name' in command:
nameTell(command)
elif 'open classroom' in command:
openClassroom()
elif 'hello' in command:
greetingHello()
elif 'weather' in command:
weather()
elif 'google search' in command:
googleSearch(command)
elif 'search google' in command:
googleSearch(command)
elif 'who am i' in command:
whoami()
elif 'tell me a joke' in command:
tellAJoke()
elif 'open my computer' in command:
openMyComp()
elif 'open calculator' in command:
openCalc()
elif 'open word' in command:
openMSword()
elif 'word' in command:
wordProcess()
elif 'open excel' in command:
openMSExcel()
elif 'edit excel' in command:
workWithExcel()
elif 'open power point' in command:
openMSppt()
elif 'news for today' in command:
print("Getting today's new for you...")
voiceOutput("Getting today's new for you.")
webbrowser.open("https://news.google.com")
elif 'goodbye' in command:
print("Goodbye. Have a nice day!!")
voiceOutput("Goodbye. Have a nice day!")
break
greetings()
run_assistant()