Linux 有没有办法直接将python输出发送到剪贴板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7606062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to directly send a python output to clipboard?
提问by nye17
For example, if a python script will spit out a string giving the path of a newly written file that I'm going to edit immediately after running the script, it would be very nice to have it directly sent to the system clipboard rather than STDOUT
.
例如,如果一个 python 脚本会吐出一个字符串,给出一个新编写的文件的路径,我将在运行脚本后立即编辑该文件,那么将它直接发送到系统剪贴板而不是STDOUT
.
采纳答案by NullUserException
You can use an external program, xsel
:
您可以使用外部程序xsel
:
from subprocess import Popen, PIPE
p = Popen(['xsel','-pi'], stdin=PIPE)
p.communicate(input='Hello, World')
With xsel
, you can set the clipboard you want to work on.
使用xsel
,您可以设置要处理的剪贴板。
-p
works with thePRIMARY
selection. That's the middle click one.-s
works with theSECONDARY
selection. I don't know if this is used anymore.-b
works with theCLIPBOARD
selection. That's yourCtrl + V
one.
-p
与PRIMARY
选择一起工作。那是中间的点击之一。-s
与SECONDARY
选择一起工作。不知道这个有没有用。-b
与CLIPBOARD
选择一起工作。那是你的Ctrl + V
。
Read more about X's clipboards hereand here.
A quick and dirty function I created to handle this:
我创建了一个快速而肮脏的函数来处理这个问题:
def paste(str, p=True, c=True):
from subprocess import Popen, PIPE
if p:
p = Popen(['xsel', '-pi'], stdin=PIPE)
p.communicate(input=str)
if c:
p = Popen(['xsel', '-bi'], stdin=PIPE)
p.communicate(input=str)
paste('Hello', False) # pastes to CLIPBOARD only
paste('Hello', c=False) # pastes to PRIMARY only
paste('Hello') # pastes to both
You can also try pyGTK's clipboard
:
你也可以试试 pyGTK 的clipboard
:
import pygtk
pygtk.require('2.0')
import gtk
clipboard = gtk.clipboard_get()
clipboard.set_text('Hello, World')
clipboard.store()
This works with the Ctrl + V
selection for me.
这适用Ctrl + V
于我的选择。
回答by kindall
This is not really a Python question but a shell question. You already can send the output of a Python script (or any command) to the clipboard instead of standard out, by piping the output of the Python script into the xclip
command.
这不是一个真正的 Python 问题,而是一个 shell 问题。通过将 Python 脚本的输出通过管道传输到命令中,您已经可以将 Python 脚本(或任何命令)的输出发送到剪贴板而不是标准输出xclip
。
myscript.py | xclip
If xclip
is not already installed on your system (it isn't by default), this is how you get it:
如果xclip
您的系统上尚未安装(默认情况下不是),您可以通过以下方式获得它:
sudo apt-get install xclip
If you wanted to do it directly from your Python script I guess you could shell out and run the xclip command using os.system()
which is simple but deprecated. There are a number of ways to do this (see the subprocess
module for the current official way). The command you'd want to execute is something like:
如果您想直接从 Python 脚本执行此操作,我想您可以使用os.system()
它来运行 xclip 命令,该命令很简单但已弃用。有多种方法可以做到这一点(请参阅subprocess
当前官方方法的模块)。您要执行的命令类似于:
echo -n /path/goes/here | xclip
Bonus: Under Mac OS X, you can do the same thing by piping into pbcopy
.
奖励:在 Mac OS X 下,您可以通过管道输入pbcopy
.
回答by Stefano Mtangoo
As others have pointed out this is not "Python and batteries" as it involves GUI operations. So It is platform dependent. If you are on windows you can use win32 Python Module and Access win32 clipboard operations. My suggestion though would be picking up one GUI toolkit (PyQT/PySide for QT, PyGTK for GTK+ or wxPython for wxWidgets). Then use the clipboard operations. If you don't need the heavy weight things of toolkits then make your wrapper which will use win32 package on windows and whatever is available on other platform and switch accordingly!
正如其他人指出的那样,这不是“Python 和电池”,因为它涉及 GUI 操作。所以它依赖于平台。如果您在 Windows 上,您可以使用 win32 Python 模块和访问 win32 剪贴板操作。不过我的建议是选择一个 GUI 工具包(用于 QT 的 PyQT/PySide、用于 GTK+ 的 PyGTK 或用于 wxWidgets 的 wxPython)。然后使用剪贴板操作。如果您不需要工具包的重量级东西,那么制作您的包装器,它将在 Windows 上使用 win32 包以及其他平台上可用的任何包,并相应地进行切换!
For wxPython here are some helpful links:
对于 wxPython,这里有一些有用的链接:
http://www.wxpython.org/docs/api/wx.Clipboard-class.html
http://www.wxpython.org/docs/api/wx.Clipboard-class.html
http://wiki.wxpython.org/ClipBoard
http://wiki.wxpython.org/ClipBoard
http://www.python-forum.org/pythonforum/viewtopic.php?f=1&t=25549
http://www.python-forum.org/pythonforum/viewtopic.php?f=1&t=25549
回答by Marcelo Lacerda
As it was posted in another answer, if you want to solve that within python, you can use Pyperclipwhich has the added benefit of being cross-platform.
正如在另一个答案中发布的那样,如果您想在 python 中解决这个问题,您可以使用Pyperclip,它具有跨平台的额外好处。
>>> import pyperclip
>>> pyperclip.copy('The text to be copied to the clipboard.')
>>> pyperclip.paste()
'The text to be copied to the clipboard.'