Detailed explanation of Python’s idea of querying clipboard automatic matching information

Time:2022-1-14

I don’t want to check EXECL every time. I want to be more convenient and faster.

Popular idea: click exe, python will automatically monitor the contents of the clipboard, and then regularly take out the IP. Then compare the business documents according to the IP to obtain the corresponding information, and then pop up the prompt to write the queried contents to the clipboard.

'''
Function: compare clipboard class content
'''
 
import win32clipboard as w
import win32con
import xlrd
from tkinter import messagebox
import win32api, win32con
import pyperclip
import re
import sys
import os
 
# print(__file__)
 
path = os.path.dirname(os.path.abspath(__file__))
sys.intern(path)
 
 
# print(path)
#Gets the contents of the clipboard
def getText():
    w.OpenClipboard()
    d = w.GetClipboardData(win32con.CF_TEXT)
    w.CloseClipboard()
    return (d).decode('GBK')
 
 
#Sets the class content of the clipboard
def set_text(aString):
    w.OpenClipboard()
    w.EmptyClipboard()
    w.SetClipboardData(win32con.CF_TEXT, aString)
    w.CloseClipboard()
 
 
#Generate resource file directory access path
def resource_path(relative_path):
    If getattr (sys, 'frozen', false): # whether the bundle resource
        base_path = sys._MEIPASS
    else:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)
 
 
#Get the IP in the clipboard and judge whether it is normal
def get_ip(ss_ip):
    ipList = re.findall(r'[0-9]+(?:\.[0-9]+){3}', ss_ip)
    # print(ipList)
    if ipList:
        return ipList
    else:
        win32api. MessageBox (0, "please check whether the replication has IP, please retest", "reminder", win32con.mb_ok)
        sys.exit(0)
 
 
#Get the data in XLS and compare it with the previous clipboard data
def host(ss_ip):
    #Get the content of EXECL, which is analyzed according to the business
    filename = resource_path(os.path.join("res", "hosts.xls"))
    # print(filename)
    # execl_hosts = './hosts.xls'
    data1 = xlrd.open_workbook(filename)
    page = data1.sheet_by_index(2)
    nrows1 = page.nrows
    ncols1 = page.ncols
    #Get IP
    host_ip = page.col_values(10)
    app = page. col_ Values (1) # function cluster
    purpose = page. col_ Values (2) # purpose
    hostname = page. col_ Values (11) # host name
    # print(host_ip)
 
    #Start comparing data
    start = 0
    count = 1
    # print(ss_ip)
    if str(ss_ip[0]) not in host_ip:
        win32api. MessageBox (0, F "no information about device {ss_ip [0]}", "unknown device", win32con.mb_ok)
        sys.exit(0)
    for k, item in enumerate(host_ip, start):
        # print(k,item,ss_ip[0])
        if str(ss_ip[0]) == str(item):
            #Print ("normal:" + item, K)
            win32api. MessageBox (0, F "\ t \ tnote \ n host IP: {item} host name: {hostname [k]} \ n function cluster: {app [k]} host purpose: {purpose [k]}",
                                "Discover device", win32con MB_ OK)
            pyperclip. Copy (F "host IP: {item} host name: {hostname [k]} \ nfunctional cluster: {app [k]} host purpose: {purpose [k]}")
            sys.exit(0)
 
        count = count + 1
 
 
def main():
    ss_ip = getText()
    one_ip = get_ip(ss_ip)
    host(one_ip)
 
 
if __name__ == '__main__':
    main()

Test effect:

Package resource generation exe

Python packaging Exe has four methods: py2exe, pyinstaller and Cx_ Freeze and nuitka. The most commonly used is pyinstaller. Pyinstaller itself is not a python library, but it can still be installed and generated by installing the python library Exe can be used across multiple platforms and can also specify icons.

We need to put all the resource files we use in one folder. In this paper, a new subfolder named res is created in the current directory to store the resource file. This paper assumes that the resource file in res is hosts xls

After modification Py file can be run first to ensure no error. Then use the CMD command:

pyi-makespec -F beloved.py

Generate Spec file. If you want to add Icon, you can use PYI makespec — icon ABC jpg -F beloved. Py statement generates a spec file.
Next, modify Spec file:

Before modification, data = [], this article changes it to the figure above, which means

Will be loved Py the res directory under the current directory (and the files in the directory) is added to the target exe and placed in the root directory of the zero time file at runtime with the name res.

Generate Exe file and other related files

Next, we can safely generate Exe file. Execute CMD command

pyinstaller -F beloved.spec

The. EXE file is generated in the sub file dict. That’s it Exe was sent to other computers to run Exe runs slowly. It is recommended to wait more. As long as there is no error prompt, it is OK.

Reference address: https://blog.csdn.net/qq_44685030/article/details/105096338

This is the end of this article about Python’s implementation of querying clipboard automatic matching information. For more information about Python’s query clipboard, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!