有没有办法直接将 python 输出发送到剪贴板?
例如,如果一个 python 脚本会吐出一个字符串,给出一个新编写的文件的路径,我将在运行该脚本后立即编辑该文件,那么将其直接发送到系统剪贴板而不是 <代码>STDOUT。
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用外部程序
xsel
:
xsel
,您可以设置您想要处理的剪贴板。-p
适用于PRIMARY
选择。这是中间点击的一个。-s
与SECONDARY
选择一起使用。我不知道现在还用不着这个。-b
适用于CLIPBOARD
选择。这就是您的Ctrl + V
。了解有关 X 剪贴板的更多信息此处和此处。
我创建了一个快速而肮脏的函数来处理这个问题:
您还可以尝试 pyGTK 的
剪贴板
:这适用于我的
Ctrl + V
选择。You can use an external program,
xsel
:With
xsel
, you can set the clipboard you want to work on.-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.Read more about X's clipboards here and here.
A quick and dirty function I created to handle this:
You can also try pyGTK's
clipboard
:This works with the
Ctrl + V
selection for me.正如它发布在另一个答案中,如果你想在python中解决这个问题,你可以使用Pyperclip 具有跨平台的额外好处。
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问题,而是一个shell问题。您已经可以通过将 Python 脚本的输出传送到 xclip 命令中,将 Python 脚本(或任何命令)的输出发送到剪贴板而不是标准输出。
如果您的系统上尚未安装 xclip(默认情况下未安装),则可以通过以下方式获取它:
如果您想直接从 Python 脚本执行此操作,我想您可以 shell 并使用 os.system() 运行 xclip 命令,该命令很简单,但已弃用。有多种方法可以做到这一点(请参阅
subprocess
模块了解当前的官方方法)。您想要执行的命令类似于:额外的好处:在 Mac OS X 下,您可以通过管道输入
pbcopy
来执行相同的操作。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.If
xclip
is not already installed on your system (it isn't by default), this is how you get it: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 thesubprocess
module for the current official way). The command you'd want to execute is something like:Bonus: Under Mac OS X, you can do the same thing by piping into
pbcopy
.正如其他人指出的那样,这不是“Python 和电池”,因为它涉及 GUI 操作。所以它是平台相关的。如果您使用的是 Windows,则可以使用 win32 Python 模块并访问 win32 剪贴板操作。
不过,我的建议是选择一个 GUI 工具包(用于 QT 的 PyQT/PySide、用于 GTK+ 的 PyGTK 或用于 wxWidgets 的 wxPython)。然后使用剪贴板操作。如果您不需要工具包的重量级东西,那么制作您的包装器,它将在 Windows 上使用 win32 包以及其他平台上可用的任何东西,并相应地进行切换!
对于 wxPython,这里有一些有用的链接:
http://www.wxpython.org /docs/api/wx.Clipboard-class.html
http://wiki.wxpython.org/ClipBoard
http://www.python-forum.org/pythonforum/viewtopic.php?f=1&t=25549
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!
For wxPython here are some helpful links:
http://www.wxpython.org/docs/api/wx.Clipboard-class.html
http://wiki.wxpython.org/ClipBoard
http://www.python-forum.org/pythonforum/viewtopic.php?f=1&t=25549