有没有一种方法可以直接将python输出发送到剪贴板?

编程入门 行业动态 更新时间:2024-10-08 10:52:20
本文介绍了有没有一种方法可以直接将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.

推荐答案

您可以使用外部程序 xsel :

You can use an external program, xsel:

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

使用xsel,您可以设置要使用的剪贴板.

With xsel, you can set the clipboard you want to work on.

  • -p与PRIMARY选择一起使用.那是中键单击.
  • -s与SECONDARY选择一起使用.我不知道是否再使用了.
  • -b与CLIPBOARD选择一起使用.那就是你的Ctrl + V.
  • -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.

在此处和此处.

我创建的一种快速而肮脏的功能来处理此问题:

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) pmunicate(input=str) if c: p = Popen(['xsel', '-bi'], stdin=PIPE) pmunicate(input=str) paste('Hello', False) # pastes to CLIPBOARD only paste('Hello', c=False) # pastes to PRIMARY only paste('Hello') # pastes to both

您还可以尝试pyGTK的 clipboard :

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()

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

更多推荐

有没有一种方法可以直接将python输出发送到剪贴板?

本文发布于:2023-11-25 21:04:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:剪贴板   可以直接   发送到   方法   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!