python 复制字符串到剪贴板

回复
头像
hou
网站管理员
帖子: 1313
注册时间: 2019年 5月 21日 08:36

python 复制字符串到剪贴板

帖子 hou » 2019年 8月 29日 09:01

方法一
使用pyperclip
import pyperclip
pyperclip.copy("Hello world! This is copied to clipboard via pyperclip")
pyperclip.paste()

方法二
使用clipboard
import clipboard
clipboard.copy("abc") # now the clipboard content will be string "abc"
text = clipboard.paste() # text will have the content of clipboard

方法三
在tkinter中
from Tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append('i can has clipboardz?')
r.update() # now it stays on the clipboard after the window is closed
r.destroy()

方法四
在pandas中的复制
import pandas as pd
df=pd.DataFrame(['Text to copy'])
df.to_clipboard(index=False,header=False)

方法五
windows API
import ctypes

# Get required functions, strcpy..
strcpy = ctypes.cdll.msvcrt.strcpy
ocb = ctypes.windll.user32.OpenClipboard # Basic clipboard functions
ecb = ctypes.windll.user32.EmptyClipboard
gcd = ctypes.windll.user32.GetClipboardData
scd = ctypes.windll.user32.SetClipboardData
ccb = ctypes.windll.user32.CloseClipboard
ga = ctypes.windll.kernel32.GlobalAlloc # Global memory allocation
gl = ctypes.windll.kernel32.GlobalLock # Global memory Locking
gul = ctypes.windll.kernel32.GlobalUnlock
GMEM_DDESHARE = 0x2000

def Get():
ocb(None) # Open Clip, Default task

pcontents = gcd(1) # 1 means CF_TEXT.. too lazy to get the token thingy...

data = ctypes.c_char_p(pcontents).value

#gul(pcontents) ?
ccb()

return data

def Paste(data):
ocb(None) # Open Clip, Default task

ecb()

hCd = ga(GMEM_DDESHARE, len(bytes(data,"ascii")) + 1)

pchData = gl(hCd)

strcpy(ctypes.c_char_p(pchData), bytes(data, "ascii"))

gul(hCd)

scd(1, hCd)

ccb()

改进版本, 增加对emoji的支持
import ctypes

OpenClipboard = ctypes.windll.user32.OpenClipboard
EmptyClipboard = ctypes.windll.user32.EmptyClipboard
GetClipboardData = ctypes.windll.user32.GetClipboardData
SetClipboardData = ctypes.windll.user32.SetClipboardData
CloseClipboard = ctypes.windll.user32.CloseClipboard
CF_UNICODETEXT = 13

GlobalAlloc = ctypes.windll.kernel32.GlobalAlloc
GlobalLock = ctypes.windll.kernel32.GlobalLock
GlobalUnlock = ctypes.windll.kernel32.GlobalUnlock
GlobalSize = ctypes.windll.kernel32.GlobalSize
GMEM_MOVEABLE = 0x0002
GMEM_ZEROINIT = 0x0040

unicode_type = type(u'')

def get():
text = None
OpenClipboard(None)
handle = GetClipboardData(CF_UNICODETEXT)
pcontents = GlobalLock(handle)
size = GlobalSize(handle)
if pcontents and size:
raw_data = ctypes.create_string_buffer(size)
ctypes.memmove(raw_data, pcontents, size)
text = raw_data.raw.decode('utf-16le').rstrip(u'\0')
GlobalUnlock(handle)
CloseClipboard()
return text

def put(s):
if not isinstance(s, unicode_type):
s = s.decode('mbcs')
data = s.encode('utf-16le')
OpenClipboard(None)
EmptyClipboard()
handle = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, len(data) + 2)
pcontents = GlobalLock(handle)
ctypes.memmove(pcontents, data, len(data))
GlobalUnlock(handle)
SetClipboardData(CF_UNICODETEXT, handle)
CloseClipboard()

paste = get
copy = put

作者:WangLane
链接:https://www.jianshu.com/p/27c7f6647abc
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

头像
hou
网站管理员
帖子: 1313
注册时间: 2019年 5月 21日 08:36

Re: python 复制到剪贴板

帖子 hou » 2019年 9月 2日 03:29

我用了clipboard,但是手机不支持

回复

回到 “python”