简体   繁体   English

有没有办法直接将python输出发送到剪贴板?

[英]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. -bCLIPBOARD选择一起使用。 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脚本的输出xclipxclip命令。

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.

相关问题 直接将`print`的输出复制到剪贴板 - Copy the output of `print` to clipboard directly SQLite和Python:是否可以直接将输出转换为列表? - SQLite and Python: Is there a way I can directly convert output to a list? 任何将python脚本的输出直接传输到.txt文件的方法 - any way to transfer output of a python script directly into a .txt file 如何在python中将图像发送到剪贴板? - How can I send an image to the clipboard in python? python可以将文本发送到Mac剪贴板吗 - Can python send text to the Mac clipboard 如何在Python中将数学输出复制到剪贴板? - How to copy a math output to clipboard in Python? 如何将Curl的输出直接发送到Postgres数据库 - How to send the output of Curl directly to a postgres databse 有没有办法通过python用斜体格式化剪贴板? - Is there a way to format the clipboard with italics through python? 有没有一种方法可以在Python运行中将剪贴板内容转换为列表? - Is there a way to convert the clipboard content into list in the run in Python? 有没有办法在网站上嵌入使用 python 的 Networkx package 图形的代码,以便 output 可以直接显示在网站上? - Is there a way to embed code that uses python's Networkx package for graphs on a website so that the output can be displayed directly on the website?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM