Suppose you have a boring task to fill in many forms in a web page or software containing some text fields. The clipboard saves you from entering the same text again and again, but there is only one content on the clipboard at a time. If you have several different pieces of text to copy and paste, you have to mark and copy several of the same content again and again. This boring job almost breaks down.
Fortunately, you have learned python, python is the most suitable for this kind of work.
The pyperclip
module is required to copy and paste, and the sys
module is required to read command line parameters. The shelve
module will be used to save the variables in the Python program to the binary shelf
file. The shelve
module allows you to add "save" and "open" functions to the program to facilitate the loading of variables the next time the program is run.
The procedure to do is as follows:
Assuming you are using the windows environment, to run the program, you need to create a bat
batch program, and use the run window called up by the key combination win + R
to run the program. The contents of the batch file of bat
are as follows:
@ pythonw.exe D:\python\ch00_book\mcb.pyw %* :: Just replace the path at runtime
Decomposing the program can effectively help us to write the program. Let us write a scripting framework, which looks like this.
#! python3
# mcb.pyw -The name of the program, used to save and load multiple clipboards
# Import the used modules
import shelve, pyperclip, sys
# Initialize shelf file mcb.
mcbShelf = shelve.open('mcb')
# Get command line parameters
command = sys.argv[1].lower()
# TODO:Save the clipboard content and set a keyword for each copied content.
# TODO:List all keywords.
# TODO:Delete a keyword and clear the content corresponding to the keyword.
# TODO:Delete all keywords and clear the clipboard.
# TODO:According to the command line parameters, display the content corresponding to a keyword.
mcbShelf.close()
The commands for each step are supplemented in turn below
if command =='save':
mcbShelf[sys.argv[2]]= pyperclip.paste()
elif command =='list':
pyperclip.copy(", ".join(mcbShelf.keys()))
elif command =='delete':
del mcbShelf[sys.argv[2]]
elif command =='delete_all':
# Empty shelf file
mcbShelf.clear()
# mcbShelf = shelve.open('mcb', flag='n')
# Empty the clipboard
pyperclip.copy('')
elif sys.argv[1]in mcbShelf:
pyperclip.copy(mcbShelf[sys.argv[1]])else:
pyperclip.copy('Sorry, You have input a wrong keyword.')
import shelve
import pyperclip
import sys
mcbShelf = shelve.open('mcb')
command = sys.argv[1].lower()if command =='save':
mcbShelf[sys.argv[2]]= pyperclip.paste()
elif command =='list':
pyperclip.copy(", ".join(mcbShelf.keys()))
elif command =='delete':
del mcbShelf[sys.argv[2]]
elif command =='delete_all':
# Empty shelf file
mcbShelf.clear()
# mcbShelf = shelve.open('mcb', flag='n')
# Empty the clipboard
pyperclip.copy('')
elif sys.argv[1]in mcbShelf: #If the command line parameter is a keyword
pyperclip.copy(mcbShelf[sys.argv[1]])else: #If the command line parameter is not a keyword
pyperclip.copy('Sorry, You have input a wrong keyword.')
mcbShelf.close()
Recommended Posts