[英]Store files in clipboard in python (cross-platform)
I'm trying to store a file in the clipboard in windows mac and linux.我正在尝试将文件存储在 windows mac 和 linux 的剪贴板中。 For ex: running the script will put a file / files in the clipboard and now I can ctrl+v wherever i want to paste the files.例如:运行脚本会将文件/文件放在剪贴板中,现在我可以 ctrl+v 粘贴文件的任何位置。
for windows i have written this code in c#:对于 windows,我在 c# 中编写了此代码:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace file2clip
{
class Program
{
[STAThread]
static void Main(string[] args)
{
StringCollection paths = new StringCollection();
foreach (string s in args) paths.Add(s);
Clipboard.SetFileDropList(paths);
}
}
}
then implement in python:然后在 python 中实现:
import subprocess
def files_to_win_clip(files: tuple):
subprocess.call(f'file2clip.exe {files}')
For linux i found xclip which allows to copy and paste files but it doesnt go to the default clipboard so I can't paste (with ctrl+v) the file wherever I'm at.对于 linux,我找到了允许复制和粘贴文件的 xclip,但它不会 go 到默认剪贴板,因此我无法将文件粘贴(使用 ctrl+v)无论我在哪里。
I might need some help on how to make it work on linux and mac.我可能需要一些关于如何使它在 linux 和 mac 上工作的帮助。
There are a lot of python modules to do this.有很多 python 模块可以做到这一点。 One of them is https://pypi.org/project/pyperclip/其中之一是https://pypi.org/project/pyperclip/
The c program in this way is not needed you can read the file in python as text or do all logic that you want and put it in the clipboard. c 程序不需要这种方式,您可以将 python 中的文件作为文本读取或执行所有您想要的逻辑并将其放入剪贴板。
For full file copy you can use this snippet:对于完整的文件副本,您可以使用此代码段:
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])
data = QtCore.QMimeData()
url = QtCore.QUrl.fromLocalFile('c:\\foo.file')
data.setUrls([url])
app.clipboard().setMimeData(data)
I can't comment but xclip
can be used as follows:我无法发表评论,但xclip
可以按如下方式使用:
$ xclip -selection clipboard [FILES]
This copies the files' text to the clipboard;这会将文件的文本复制到剪贴板; I'm not sure if this is the behaviour you are looking for though.我不确定这是否是您正在寻找的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.