python writes and gets an instance of the clipboard content

  • 2020-10-31 21:51:31
  • OfStack

Writing desktop programs or special operations often requires access to the clipboard. python has dedicated modules that make it easy and simple to operate the shears

As follows:


#coding:utf-8
import win32clipboard as w
import win32con
# Gets the clipboard contents 
def gettext():
 w.OpenClipboard()
 t = w.GetClipboardData(win32con.CF_TEXT)
 w.CloseClipboard()
 return t
# Writes the clipboard contents 
def settext(aString):
 w.OpenClipboard()
 w.EmptyClipboard()
 w.SetClipboardData(win32con.CF_TEXT, aString)
 w.CloseClipboard()
a = "hello python"
settext(a)
print gettext()

The above small program will print the hello python character when run.

You can try to unwrite the clipboard code, then use Ctrl+c to copy 1 character, and then run the program.

Or log out to get the clipboard code, run the program and then use Ctrl+v to paste.

You can see the results more intuitively


Related articles: