Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a personal assistant program with Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to write a personal assistant program in Python, which may not be well understood by many people. In order to let everyone know more, Xiaobian summarized the following contents for everyone. I hope everyone can gain something according to this article.

Are there any tasks in your job that you do repeatedly? That's the fun of programming. With some thinking and programming, you can automate your tasks and save you a lot of time.

1. assistant function

We want to create an assistant that can write emails. Just type in the recipient's name or nickname, and it handles the rest. To write a good email, the program looks at Excel spreadsheets we've created with basic information about people I'd normally email to every week. For example, if I type Caleb(the name of whom I want to email), it copies the following for me:

Full name: Caleb (Coco) Stephano Email to send to: stevens.coco12345@snailmail.com Hi Coco, I hope all is well on the UX team! Thanks, Ben

2. input program instruction

In order for your Python helper to help you, you may need to make some requests to it. First, prompt the user for text:

answer=input('What can I help you with? Enter here: ')

When you run your file, you will notice that in the terminal or command prompt, the cursor is placed right behind the text enclosed in parentheses above, ready for your input.

3. Get data from Excel using openpyxl

Suppose you have information about all your friends in an Excel spreadsheet. More specifically, their first and last names, nicknames, e-mail addresses, hobbies, etc. You can use openpyxl to extract data from this worksheet so that your Python helper can use it:

import openpyxl book = openpyxl.load_workbook(r'Put_Your_Path_To_Sheet_Here') ws = book.active

You can now use ws to do all sorts of things. For example, let's use our friend's example and imagine that each column is a list of information:

This list says Beth loves to play tennis. However, we shouldn't open and search our forms every time to remember this. Our assistant will help us. For example, the following additional code stores column nickname data into an array. This way, your assistant can search and manipulate your data:

import openpyxl #SET UP SHEET book = openpyxl.load_workbook(r'Put_Your_Path_To_Sheet_Here') ws = book.active #SET UP ARRAY TO HOLD NICKNAMES nickArray=[] #APPEND NICKNAMES IN LIST, RUNNING THROUGH COLUMN B OF THE SHEET UNTIL THERE'S AN EMPTY CELL skip=True firstRow=True for cell in ws['B']: if (cell.value==None): continue if (skip==False): nickArray.append(cell.value) firstRow=False skip=False #PRINT ALL ITEMS IN THAT ROW TO SEE THAT IT WORKS for x in nickArray: print(x)

This code looks at column B of the worksheet, skips the first row (usually the header row), and then scans the entire row of data until there is nothing left. It adds each item to the array.

4. Using tkinter to display user-friendly messages

On computers, people don't usually bury themselves in terminals or command prompts. Usually, you'll be using some nice user interface, like Microsoft Word or Google Chrome. You may notice that you interact with these programs through pretty windows with buttons. This is where good UX/UI comes into play.

So far, you and the assistant have communicated in terminals or command prompts, which is not an ideal UI. Python's tkinter library contains tools that allow you to create new and improved UIs.

Here is a simple example. Suppose that when we run the update.py file to update our assistant with the latest Excel information, we want to display an "update Successful" message:

#THIS IS IN updater.py from tkinter import * #DISPLAY SUCCESS MESSAGE root=Tk() labelfont=('times', 20, 'bold') root.title('Success Confirmation') successText='Your update was successful' widget=Label(root, text=successText, wraplength=600, justify=LEFT) widget.config(height=35, width=90) widget.pack(expand=YES, fill=BOTH) root.mainloop()

It works by setting the tkinter root directory as the basic window building block and adding extra detail to it. I added titles, body text, details about how the text looks, window size, and other specifications about window functionality. When you run this file, the end result looks something like this:

5. Copy text to clipboard using pyperclip

The assistant who drafted my e-mail would show me the text. Use the pyperclip library, which has tools to automatically copy any text to the clipboard:

#THIS IS IN assistant.py import pyperclip myText="Hi Coco,\n\n"+"I hope all is well on the UX team!\ n\n"+"Thanks,\n"+"Ben" pyperclip.copy(myText)

6. Write text to log

What if you want to keep a log of the last time your assistant helped you? Create a new Python file called log.py. This is the journal. If your assistant has the following code, you can always populate your log with date and time:

#THIS IS IN assistant.py #IMPORT LIBRARY THAT GETS CURRENT DATE AND TIME import datetime #CREATE AND NEW DATE AND TIME now = datetime.datetime.now() #WRITE DATE AND TIME TO THE LOG with open("log.py", "w") as f1: f1.writelines(now.strftime("%Y-%m-%d %H:%M:%S") Having read this, do you have any idea how to write a personal assistant program in Python? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report