Topic
Suppose you have a boring task to populate with a Web page or many tables in the software that contain some text fields. The Clipboard lets you not have to enter the same text again and again, but only one content at a time on the Clipboard. If you have a couple of different pieces of text that you need to copy and paste, you have to mark and copy several of the same items again and again.
You can write a Python program that tracks several paragraphs of text. This "multi-clipboard" will be named
Mcb.pyw (because "MCB" is simpler than entering "Multiclipboard"). The. pyw extension means that the terminal window is not displayed when Python runs the program.
The program saves each piece of clipboard text with a keyword. For example, when you run the py mcb.pyw save spam, the current contents of the Clipboard are saved with the keyword spam. By running the PY mcb.pyw spam, this text will be reloaded into the clipboard later. If the user forgets which keywords they have, they can run the PY mcb.pyw list and copy the lists of all the keywords to the clipboard.
Here's what the program does:
? Provides command-line arguments for the keyword to check.
? If the argument is save, the contents of the Clipboard are saved to the keyword.
? If the parameter is list, all the keywords are copied to the Clipboard.
? Otherwise, the text corresponding to the keyword is copied to the Clipboard.
This means that the code needs to do the following things:
? Read command-line arguments from SYS.ARGV.
? Read and write the Clipboard.
? Save and load the shelf file.
Code
#! python3# mcb.pyw - Saves and loads pieces of text to the clipboard.# Usage: py.exe mcb.pyw save <keyword> - Saves clipboard to keyword.# py.exe mcb.pyw <keyword> - Loads keyword to clipboard.# py.exe mcb.pyw list - Loads all keywords to clipboard.# shelve是用来保存shelf文件中import shelve, pyperclip, sysmcbShelf = shelve.open('mcb')# 保存剪切板内容if len(sys.argv) == 3 and sys.argv[1].lower() == 'save': mcbShelf[sys.argv[2]] = pyperclip.paste()elif len(sys.argv) == 2: # List keywords and load content. if sys.argv[1].lower() == 'list': pyperclip.copy(str(list(mcbShelf.keys()))) elif sys.argv[1] in mcbShelf: pyperclip.copy(mcbShelf[sys.argv[1]])mcbShelf.close()
Python Practice Exercise: multiple clipboard