简体   繁体   中英

Is there a way to directly send a python output to clipboard?

例如,如果一个python脚本会吐出一个字符串,该字符串给出了我将在运行脚本后立即编辑的新编写文件的路径,那么将它直接发送到系统剪贴板而不是STDOUT会非常好。

You can use an external program, 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.

  • -p works with the PRIMARY selection. That's the middle click one.
  • -s works with the SECONDARY selection. I don't know if this is used anymore.
  • -b works with the CLIPBOARD selection. That's your Ctrl + V one.

Read more about X's clipboards here and 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 :

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.

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.

myscript.py | xclip

If xclip is not already installed on your system (it isn't by default), this is how you get it:

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:

echo -n /path/goes/here | xclip

Bonus: Under Mac OS X, you can do the same thing by piping into 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.

>>> 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM