How do I read text from the (windows) clipboard from python?
如何从python的(Windows)剪贴板中读取文本?
您可以使用称为win32clipboard的模块,该模块是pywin32的一部分。
这是一个示例,该示例首先设置剪贴板数据然后获取它:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import win32clipboard # set clipboard data win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardText('testing 123') win32clipboard.CloseClipboard() # get clipboard data win32clipboard.OpenClipboard() data = win32clipboard.GetClipboardData() win32clipboard.CloseClipboard() print data |
文档中的重要提示:
When the window has finished examining or changing the clipboard,
close the clipboard by calling CloseClipboard. This enables other
windows to access the clipboard. Do not place an object on the
clipboard after calling CloseClipboard.
您可以通过内置模块Tkinter轻松完成此操作,该模块基本上是一个GUI库。此代码创建一个空白窗口小部件以从OS获取剪贴板内容。
1 2 3 | #from tkinter import Tk # Python 3 from Tkinter import Tk Tk().clipboard_get() |
我已经看到了许多使用win32模块的建议,但是Tkinter提供了我所见过的最短,最简单的方法,如本文中所示:如何使用Python将字符串复制到Windows剪贴板上?
另外,Tkinter在python标准库中。
如果您不想安装额外的软件包,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import ctypes CF_TEXT = 1 kernel32 = ctypes.windll.kernel32 kernel32.GlobalLock.argtypes = [ctypes.c_void_p] kernel32.GlobalLock.restype = ctypes.c_void_p kernel32.GlobalUnlock.argtypes = [ctypes.c_void_p] user32 = ctypes.windll.user32 user32.GetClipboardData.restype = ctypes.c_void_p def get_clipboard_text(): user32.OpenClipboard(0) try: if user32.IsClipboardFormatAvailable(CF_TEXT): data = user32.GetClipboardData(CF_TEXT) data_locked = kernel32.GlobalLock(data) text = ctypes.c_char_p(data_locked) value = text.value kernel32.GlobalUnlock(data_locked) return value finally: user32.CloseClipboard() print(get_clipboard_text()) |
上面最受支持的答案很奇怪,因为它只是清除剪贴板然后获取内容(然后为空)。可以清除剪贴板以确保某些剪贴板内容类型(如"格式文本")不会"覆盖"要保存在剪贴板中的纯文本内容。
以下代码段用空格替换剪贴板中的所有换行符,然后删除所有双精度空格,最后将内容保存回剪贴板:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import win32clipboard win32clipboard.OpenClipboard() c = win32clipboard.GetClipboardData() win32clipboard.EmptyClipboard() c = c.replace(' ', ' ') c = c.replace(' ', ' ') while c.find(' ') != -1: c = c.replace(' ', ' ') win32clipboard.SetClipboardText(c) win32clipboard.CloseClipboard() |
我发现这是从python访问剪贴板的最简单方法:
1)安装pyperclip:
2)用法:
1 2 3 4 5 6 | import pyperclip s = pyperclip.paste() pyperclip.copy(s) # the type of s is string |
在Win10 64位,Python 3.5上测试。似乎也可以使用非ASCII字符。
测试的字符包括±°??αβγθΔΨΦ???
python标准库可以做到...
1 2 3 4 5 6 7 8 9 10 11 12 | try: # Python3 import tkinter as tk except ImportError: # Python2 import Tkinter as tk def getClipboardText(): root = tk.Tk() # keep the window from showing root.withdraw() return root.clipboard_get() |
对于我的控制台程序,上面的tkinter的答案对我来说不太有效,因为.destroy()始终会给出错误:
can't invoke"event" command: application has been destroyed while executing...
或使用.withdraw()时,控制台窗口没有重新获得焦点。
若要解决此问题,您还必须在.destroy()之前调用.update()。例:
1 2 3 4 5 6 7 8 | # Python 3 import tkinter r = tkinter.Tk() text = r.clipboard_get() r.withdraw() r.update() r.destroy() |
r.withdraw()会阻止框架显示一毫秒,然后框架将被破坏,从而将焦点移回控制台。
尝试使用win32all软件包中的win32clipboard(如果您使用ActiveState Python,则可能已安装)。
请参阅此处的示例:http://code.activestate.com/recipes/474121/
一个不是很直接的把戏:
使用pyautogui热键:
1 2 | Import pyautogui pyautogui.hotkey('ctrl', 'v') |
因此,您可以根据需要粘贴剪贴板数据。
使用Pythons库剪贴板
它只是这样使用:
1 2 3 | import clipboard clipboard.copy("this text is now in the clipboard") print clipboard.paste() |