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

发布于 2024-12-07 11:24:45 字数 108 浏览 0 评论 0原文

例如,如果一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沧笙踏歌 2024-12-14 11:24:45

您可以使用外部程序 xsel

from subprocess import Popen, PIPE
p = Popen(['xsel','-pi'], stdin=PIPE)
p.communicate(input='Hello, World')

xsel,您可以设置您想要处理的剪贴板。

  • -p 适用于 PRIMARY 选择。这是中间点击的一个。
  • -sSECONDARY 选择一起使用。我不知道现在还用不着这个。
  • -b 适用于 CLIPBOARD 选择。这就是您的Ctrl + V

了解有关 X 剪贴板的更多信息此处此处

我创建了一个快速而肮脏的函数来处理这个问题:

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

您还可以尝试 pyGTK 的 剪贴板

import pygtk
pygtk.require('2.0')
import gtk

clipboard = gtk.clipboard_get()

clipboard.set_text('Hello, World')
clipboard.store()

这适用于我的Ctrl + V选择。

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.

百思不得你姐 2024-12-14 11:24:45

正如它发布在另一个答案中,如果你想在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 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.'
笑梦风尘 2024-12-14 11:24:45

这实际上不是一个Python问题,而是一个shell问题。您已经可以通过将 Python 脚本的输出传送到 xclip 命令中,将 Python 脚本(或任何命令)的输出发送到剪贴板而不是标准输出。

myscript.py | xclip

如果您的系统上尚未安装 xclip(默认情况下未安装),则可以通过以下方式获取它:

sudo apt-get install xclip

如果您想直接从 Python 脚本执行此操作,我想您可以 shell 并使用 os.system() 运行 xclip 命令,该命令很简单,但已弃用。有多种方法可以做到这一点(请参阅 subprocess 模块了解当前的官方方法)。您想要执行的命令类似于:

echo -n /path/goes/here | xclip

额外的好处:在 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.

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.

并安 2024-12-14 11:24:45

正如其他人指出的那样,这不是“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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文