Using Python to implement multiple clipboards

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:

  1. Identify command line parameters;
  2. When to save the clipboard contents?
  3. When will the clipboard contents be deleted?
  4. How to display clipboard content? Show all or by keywords?
  5. How to clear the clipboard?
  6. After clearing the clipboard and pasting, what will I see?

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

Save the clipboard content and set a keyword for each copied content

if command =='save':
 mcbShelf[sys.argv[2]]= pyperclip.paste()

List all keywords

elif command =='list':
 pyperclip.copy(", ".join(mcbShelf.keys()))

Delete a keyword and clear the content corresponding to the keyword

elif command =='delete':
 del mcbShelf[sys.argv[2]]

Delete all keywords and clear the clipboard

elif command =='delete_all':
 # Empty shelf file
 mcbShelf.clear()
 # mcbShelf = shelve.open('mcb', flag='n')
 # Empty the clipboard
 pyperclip.copy('')

According to the command line parameters, display the content corresponding to a keyword

elif sys.argv[1]in mcbShelf:
 pyperclip.copy(mcbShelf[sys.argv[1]])else:
 pyperclip.copy('Sorry, You have input a wrong keyword.')

Complete program

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

Using Python to implement multiple clipboards
How to debug python program using repr
01. Introduction to Python
Introduction to Python
How to switch the hosts file using python
Detailed examples of using Python to calculate KS
Centos 6.4 python 2.6 upgrade to 2.7
Centos 6.4 python 2.6 upgrade to 2.7
Hyperparameter optimization using Python
Using Python to analyze the Guangzhou real estate market
Centos default python2.6 upgrade to
Solution to python alignment error
CentOS upgrade python2 to pyth
Python code to find bugs (2)
Python code to find bugs(7)
How to comment python code
Python code to find bugs(4)
Python code to find bugs (3)
Python code to find bugs(9)
How to learn python quickly
How to uninstall python plugin
Introduction to Python related modules
Python code to find bugs(6)
Does Python support multiple inheritance?
Python code to find bugs (1)
Python code to find bugs(8)
3 ways to encrypt Python files
How to understand python objects
Python code to find bugs(5)
How to use python tuples