Copy and paste processing python notes

In the python with a module for processing the contents of the clipboard can be copied, pyperclip module

pyperclip module copy () and paste () function, respectively, for sending the text to the clipboard of the computer, or receive text from it.

pyperclip python modules are not native. To install the module so that, for example, pip install pyperclip 

Here a simple example using this module:

1. example 1

Create a file to store the password you use multiple accounts: (py file suppose you create named: pw.py)

#! to python3
PASSWORDS = { 'In Email': 'adjofahoweghowaehgg',
'Blog': 'Oh GEAGefwegEgeghfewEGEege followed Houge hhaji',
'Luggage': '12345'}

SYS Import, pyperclip
IF len (the sys.argv) <2:
  Print ( 'Enter: py pw.py account name - to verify for query corresponding password')
  the sys.exit ()
Account = the sys.argv [. 1]
account in PASSWORDS IF:
  pyperclip.copy (PASSWORDS [account])
  Print ( 'Password for' account + + 'has been copied, paste using')
the else:
  ( 'no such recording system account') print

Run as follows:

python pw.py  blog

 

 Password for blog has been copied, please paste the

After running the program, the clipboard contents has been automatically to the top of your PC, you can use the paste directly; this method can be used to manage your account password, no matter how complex set passwords do not have to worry about memorizing, pasted directly you can use;

 

2 examples:

The above mentioned examples copy and paste the contents of the clipboard, but sometimes what we copied, we want to copy the contents added to it what we want to set their own, we use the following example to analyze:

For example, we copy some text above the computer, as follows:

List of animals
Lists of aquarium life
Lists of biologists by author abbreviation
List of cultivars

We want this text in front of each row are coupled with a '*' No.

Code:

#! python3
Import pyperclip
text = pyperclip.paste () # We will have to copy the contents of the clipboard ready to paste the computer assigns text
Lines = text.split ( '\ the n-') #split () method is used to specify a certain character split string example: spam = "hello i'm peter " spam.split ( " '") is output: [' Hello I ',' m Peter ']
for I in Range (len (Lines)):
  Lines [ i] = '*' + lines [i]

text = '\ n'.join (lines) #lines out by processing is a list of characters, and a text string is required, it is necessary to use the connection to join each character string in each list
pyperclip.copy (text)

# join on a string method invocation parameter list is a string and returns a string. The returned string concatenation of the incoming list of each string.

如:  ','.join['cats','rats','bats']

out:'cats,rats,bats' 

' 'join['cats','rats','bats']

out: 'cat rats bats'

Guess you like

Origin www.cnblogs.com/tinglele527/p/11909388.html