[英]Is there a way to directly send a python output to clipboard?
例如,如果一个python脚本会吐出一个字符串,该字符串给出了我将在运行脚本后立即编辑的新编写文件的路径,那么将它直接发送到系统剪贴板而不是STDOUT
会非常好。
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 the PRIMARY
selection. -p
适用于PRIMARY
选择。 That's the middle click one. 这是中间点击一个。 -s
works with the SECONDARY
selection. -s
适用于SECONDARY
选择。 I don't know if this is used anymore. 我不知道这是否已经被使用了。 -b
works with the CLIPBOARD
selection. -b
与CLIPBOARD
选择一起使用。 That's your Ctrl + V
one. 这是你的Ctrl + V
之一。 Read more about X's clipboards here and here . 在这里和这里阅读有关X的剪贴板的更多信息。
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
选择。
This is not really a Python question but a shell question. 这不是一个Python问题,而是一个shell问题。 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脚本(或任何命令)的输出发送到剪贴板而不是标准输出,方法是将Python脚本的输出xclip
给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. 如果您想直接从Python脚本中执行此操作,我猜您可以使用os.system()
来os.system()
并运行xclip命令,这很简单但已弃用。 There are a number of ways to do this (see the subprocess
module for the current official way). 有很多方法可以做到这一点(参见当前官方方式的subprocess
模块)。 The command you'd want to execute is something like: 您要执行的命令如下:
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
来做同样的事情。
As it was posted in another answer , if you want to solve that within python, you can use Pyperclip which 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.'
As others have pointed out this is not "Python and batteries" as it involves GUI operations. 正如其他人所指出的,这不是“Python和电池”,因为它涉及GUI操作。 So It is platform dependent. 所以它取决于平台。 If you are on windows you can use win32 Python Module and Access win32 clipboard operations. 如果你在Windows上,你可以使用win32 Python模块和Access win32剪贴板操作。 My suggestion though would be picking up one GUI toolkit (PyQT/PySide for QT, PyGTK for GTK+ or wxPython for wxWidgets). 我的建议是拿起一个GUI工具包(用于QT的PyQT / PySide,用于GTK +的PyGTK或用于wxWidgets的wxPython)。 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! 如果你不需要工具包的重量级的东西,那么制作你的包装将在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.