访问通过python复制到剪贴板的文本 [英] Accessing text copied to clipboard by python

查看:98
本文介绍了访问通过python复制到剪贴板的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我希望能够使用python脚本复制文件夹的内容,然后将这些内容粘贴到我选择的位置,即文本文件,浏览器等...我遇到了此解决方案用于将文本复制到剪贴板,但是当我实现此解决方案时,我无法粘贴任何内容。我正在使用python 3.4。以下是我正在使用的代码:

So I want to be able to use a python script to copy the contents of a folder and then be able to paste those contents to a location of my choosing i.e. text file, browser, etc... I came across this solution for copying text to the clipboard, but when i implement this solution I am not able to paste anything. I am using python 3.4. Below is code i am using:

import os 
import tkinter as tk
import tkinter.filedialog

r = tk.Tk()
r.withdraw()
photo_path= tkinter.filedialog.askdirectory(title='Which folder would you like to copy the contents from?', initialdir='/')

# Get list of filenames in current directory
file_list=[]

for filename in os.listdir(photo_path):
    if os.path.splitext(filename)[1]=='.JPG':
        file_list.append(os.path.splitext(filename)[0])
    else: pass

file_search='code:('+' OR '.join(file_list)+')'

r.clipboard_clear()
r.clipboard_append(file_search)
r.destroy()


推荐答案

在脚本结束之前不使用剪贴板内容,它会被丢弃。使其保持运行状态,直到不再需要剪贴板内容为止。以下程序将 1234 保留在剪贴板中10秒钟。如果您在这段时间内不粘贴,它将丢失。如果您在这段时间内粘贴了它,即使程序结束,它也将保留在剪贴板中。

If you don't use the clipboard content before your script ends, it is discarded. Keep it running until you no longer need the clipboard content. The following program will keep '1234' in the clipboard for 10 seconds. If you don't paste it within that time, it is lost. If you do paste it within that time, it will remain in the clipboard even after the program ends.

import tkinter as tk

r = tk.Tk()
r.withdraw()

r.clipboard_clear()
r.clipboard_append('1234')
r.after(10000, lambda: r.destroy())
r.mainloop()

这篇关于访问通过python复制到剪贴板的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆