def copy_data(self, hCtrl, key=0): # background mode
"?CVirtualGridCtrl|Custom<n>??????????????????"
if key:
self.switch_tab(self.two_way, key) # ?????('W')???('E')???('R')
start = time.time()
print("??????????????...")
# ?????????????3?...orz
for i in range(10):
time.sleep(0.3)
op.SendMessageW(hCtrl, MSG['WM_COMMAND'], MSG['COPY_DATA'], NODE['FORM'][-1])
ret = pyperclip.paste().splitlines()
if len(ret) > 1:
break
temp = (x.split('\t') for x in ret)
header = next(temp)
if '????' in header:
header.insert(header.index('????'), '??')
header.remove('????')
print('IT TAKE {} SECONDS TO GET REAL-TIME DATA'.format(time.time() - start))
return tuple(dict(zip(header, x)) for x in temp)
python类paste()的实例源码
def twitter_tweet(self):
print("1. write tweet")
print("2. copy from clipboard")
if six.PY2:
choice = raw_input('Enter choice:')
else:
choice = input('Enter choice:')
if int(choice) == 1:
if six.PY2:
tweet = raw_input('Enter tweet here:')
else:
tweet = input('Enter tweet here:')
else:
tweet = pyperclip.paste()
driver = twitter_login(self)
post_box = driver.find_element_by_id('tweet-box-home-timeline')
post_box.send_keys(tweet)
submit = driver.find_element_by_css_selector(
'button.tweet-action.EdgeButton.EdgeButton--primary.js-tweet-btn')
submit.click()
print("Posted")
return driver
def get_paste_buffer():
"""Get the contents of the clipboard / paste buffer.
:return: str - contents of the clipboard
"""
pb_str = pyperclip.paste()
# If value returned from the clipboard is unicode and this is Python 2, convert to a "normal" Python 2 string first
if six.PY2 and not isinstance(pb_str, str):
import unicodedata
pb_str = unicodedata.normalize('NFKD', pb_str).encode('ascii', 'ignore')
return pb_str
def write_to_paste_buffer(txt):
"""Copy text to the clipboard / paste buffer.
:param txt: str - text to copy to the clipboard
"""
pyperclip.copy(txt)
def replace_with_file_contents(fname):
"""Action to perform when successfully matching parse element definition for inputFrom parser.
:param fname: str - filename
:return: str - contents of file "fname"
"""
try:
with open(os.path.expanduser(fname[0])) as source_file:
result = source_file.read()
except IOError:
result = '< %s' % fname[0] # wasn't a file after all
# TODO: IF pyparsing input parser logic gets fixed to support empty file, add support to get from paste buffer
return result
def copy():
"""
Returns the current text on clipboard.
"""
data = pyperclip.paste()
## before using pyperclip
# if os.name == "posix":
# p = subprocess.Popen(["xsel", "-bo"], stdout=subprocess.PIPE)
# data = p.communicate()[0].decode("utf-8")
# elif os.name == "nt":
# data = None
# else:
# print("We don't yet support %s Operating System." % os.name)
# exit()
return data
def upload(username, password):
"""
Sends the copied text to server.
"""
payload = {"text": copy(), "device": ""}
res = requests.post(
server_url+"copy-paste/",
data = payload,
auth = (username, password)
)
if res.status_code == 200:
print("Succeses! Copied to Cloud-Clipboard.")
else:
print("Error: ", res.text)
def paste(data):
"""
Copies 'data' to local clipboard which enables pasting.
"""
# p = subprocess.Popen(["xsel", "-bi"], stdout=subprocess.PIPE,
# stdin=subprocess.PIPE)
# p = p.communicate(data.encode("utf-8"))
# if p[1] is not None:
# print("Error in accessing local clipboard")
pyperclip.copy(data)
def download(username, password):
"""
Downloads from server and updates the local clipboard.
"""
res = requests.get(server_url+"copy-paste/", auth=(username, password))
if res.status_code == 200:
paste(json.loads(res.text)["text"])
else:
print("Cannot download the data.")
def usage():
print("Error: Unknown argument")
print("Usage: cloudcb.py copy|paste|register <email> <password>")
def get_data(self):
text = pyperclip.paste()
# When the clipboard data is equal to what we copied last time, reuse
# the `ClipboardData` instance. That way we're sure to keep the same
# `SelectionType`.
if self._data and self._data.text == text:
return self._data
# Pyperclip returned something else. Create a new `ClipboardData`
# instance.
else:
return ClipboardData(
text=text,
type=SelectionType.LINES if '\n' in text else SelectionType.LINES)
def get_data(self):
text = pyperclip.paste()
# When the clipboard data is equal to what we copied last time, reuse
# the `ClipboardData` instance. That way we're sure to keep the same
# `SelectionType`.
if self._data and self._data.text == text:
return self._data
# Pyperclip returned something else. Create a new `ClipboardData`
# instance.
else:
return ClipboardData(
text=text,
type=SelectionType.LINES if '\n' in text else SelectionType.LINES)
def push_list_from_clipboard():
push_type_dict = {
u"?": 1,
u"?": 2,
u"\u2192": 3
}
def construct_push(line):
words = line.split(' ')
if len(words) < 2:
return None
push = re.sub('\\s', '', words[0])
if push not in push_type_dict:
return None
push = push_type_dict[push]
u_id = re.sub('\\s', '', words[1])
if u_id[-1] != ':':
return None
u_id = re.sub(':', '', u_id).encode('ascii')
return {'push': push, 'id': u_id}
content = pyperclip.paste()
lines = re.split('\r\n|\r|\n', content)
return [x for x in map(construct_push, lines) if x]
def determine_clipboard():
# Determine the OS/platform and set the copy() and paste() functions accordingly.
if 'cygwin' in platform.system().lower():
return init_windows_clipboard(cygwin=True)
if os.name == 'nt' or platform.system() == 'Windows':
return init_windows_clipboard()
if os.name == 'mac' or platform.system() == 'Darwin':
return init_osx_clipboard()
if HAS_DISPLAY:
# Determine which command/module is installed, if any.
try:
import gtk # check if gtk is installed
except ImportError:
pass
else:
return init_gtk_clipboard()
try:
import PyQt4 # check if PyQt4 is installed
except ImportError:
pass
else:
return init_qt_clipboard()
if _executable_exists("xclip"):
return init_xclip_clipboard()
if _executable_exists("xsel"):
return init_xsel_clipboard()
if _executable_exists("klipper") and _executable_exists("qdbus"):
return init_klipper_clipboard()
return init_no_clipboard()
def set_clipboard(clipboard):
global copy, paste
if clipboard == 'osx':
from .clipboards import init_osx_clipboard
copy, paste = init_osx_clipboard()
elif clipboard == 'gtk':
from .clipboards import init_gtk_clipboard
copy, paste = init_gtk_clipboard()
elif clipboard == 'qt':
from .clipboards import init_qt_clipboard
copy, paste = init_qt_clipboard()
elif clipboard == 'xclip':
from .clipboards import init_xclip_clipboard
copy, paste = init_xclip_clipboard()
elif clipboard == 'xsel':
from .clipboards import init_xsel_clipboard
copy, paste = init_xsel_clipboard()
elif clipboard == 'klipper':
from .clipboards import init_klipper_clipboard
copy, paste = init_klipper_clipboard()
elif clipboard == 'no':
from .clipboards import init_no_clipboard
copy, paste = init_no_clipboard()
elif clipboard == 'windows':
from .windows import init_windows_clipboard
copy, paste = init_windows_clipboard()
def main():
global recent_value
while True:
tmp_value = pyperclip.paste()
if tmp_value != recent_value:
recent_value = tmp_value
myDetect(recent_value)
time.sleep(0.1)
def get_data(self):
text = pyperclip.paste()
# When the clipboard data is equal to what we copied last time, reuse
# the `ClipboardData` instance. That way we're sure to keep the same
# `SelectionType`.
if self._data and self._data.text == text:
return self._data
# Pyperclip returned something else. Create a new `ClipboardData`
# instance.
else:
return ClipboardData(
text=text,
type=SelectionType.LINES if '\n' in text else SelectionType.LINES)
def copy_to_clipboard(text):
current = pyperclip.paste()
if (not clipboard) or (clipboard and clipboard[-1]!=current):
clipboard.append(current)
pyperclip.copy(text)
clipboard.append(text)
def clearclip(text=""):
'''
Clear the clipboard if nothing has been copied since data
was added to it
'''
if text == "":
pyperclip.copy("")
elif pyperclip.paste() == text:
pyperclip.copy("")
def get_data(self):
text = pyperclip.paste()
# When the clipboard data is equal to what we copied last time, reuse
# the `ClipboardData` instance. That way we're sure to keep the same
# `SelectionType`.
if self._data and self._data.text == text:
return self._data
# Pyperclip returned something else. Create a new `ClipboardData`
# instance.
else:
return ClipboardData(
text=text,
type=SelectionType.LINES if '\n' in text else SelectionType.LINES)
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
text = pyperclip.paste()
self.setupUi(self, text,error)
def Copy_exit(button,choice): ####called when user selects copy to clipboard
pyperclip.copy(str(choice.url))
spam = pyperclip.paste()
sys.exit()
def process(self):
while self.active:
tmp_val = pyperclip.paste()
if tmp_val != self.last_clipboard:
self.last_clipboard = tmp_val
self.copied.emit(tmp_val)
# self.window.handleClipboard(tmp_val)
time.sleep(0.1)
def Pastetext(self):
self.contents.insert(INSERT, pyperclip.paste())
def determine_clipboard():
# Determine the OS/platform and set
# the copy() and paste() functions accordingly.
if 'cygwin' in platform.system().lower():
# FIXME: pyperclip currently does not support Cygwin,
# see https://github.com/asweigart/pyperclip/issues/55
pass
elif os.name == 'nt' or platform.system() == 'Windows':
return init_windows_clipboard()
if os.name == 'mac' or platform.system() == 'Darwin':
return init_osx_clipboard()
if HAS_DISPLAY:
# Determine which command/module is installed, if any.
try:
import gtk # check if gtk is installed
except ImportError:
pass
else:
return init_gtk_clipboard()
try:
import PyQt4 # check if PyQt4 is installed
except ImportError:
pass
else:
return init_qt_clipboard()
if _executable_exists("xclip"):
return init_xclip_clipboard()
if _executable_exists("xsel"):
return init_xsel_clipboard()
if _executable_exists("klipper") and _executable_exists("qdbus"):
return init_klipper_clipboard()
return init_no_clipboard()
def set_clipboard(clipboard):
global copy, paste
clipboard_types = {'osx': init_osx_clipboard,
'gtk': init_gtk_clipboard,
'qt': init_qt_clipboard,
'xclip': init_xclip_clipboard,
'xsel': init_xsel_clipboard,
'klipper': init_klipper_clipboard,
'windows': init_windows_clipboard,
'no': init_no_clipboard}
copy, paste = clipboard_types[clipboard]()
def clipboard_read():
return paste()
def determine_clipboard():
# Determine the OS/platform and set
# the copy() and paste() functions accordingly.
if 'cygwin' in platform.system().lower():
# FIXME: pyperclip currently does not support Cygwin,
# see https://github.com/asweigart/pyperclip/issues/55
pass
elif os.name == 'nt' or platform.system() == 'Windows':
return init_windows_clipboard()
if os.name == 'mac' or platform.system() == 'Darwin':
return init_osx_clipboard()
if HAS_DISPLAY:
# Determine which command/module is installed, if any.
try:
import gtk # check if gtk is installed
except ImportError:
pass
else:
return init_gtk_clipboard()
try:
import PyQt4 # check if PyQt4 is installed
except ImportError:
pass
else:
return init_qt_clipboard()
if _executable_exists("xclip"):
return init_xclip_clipboard()
if _executable_exists("xsel"):
return init_xsel_clipboard()
if _executable_exists("klipper") and _executable_exists("qdbus"):
return init_klipper_clipboard()
return init_no_clipboard()
def set_clipboard(clipboard):
global copy, paste
clipboard_types = {'osx': init_osx_clipboard,
'gtk': init_gtk_clipboard,
'qt': init_qt_clipboard,
'xclip': init_xclip_clipboard,
'xsel': init_xsel_clipboard,
'klipper': init_klipper_clipboard,
'windows': init_windows_clipboard,
'no': init_no_clipboard}
copy, paste = clipboard_types[clipboard]()
def _read_clipboard(self):
for _ in range(15):
try:
win32api.keybd_event(17, 0, 0, 0)
win32api.keybd_event(67, 0, 0, 0)
win32api.keybd_event(67, 0, win32con.KEYEVENTF_KEYUP, 0)
win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(0.2)
return pyperclip.paste()
except Exception as e:
log.error('open clipboard failed: {}, retry...'.format(e))
time.sleep(1)
else:
raise Exception('read clipbord failed')