如何将txt文件中的内容复制到剪贴板? [英] How to copy content inside of a txt file to clipboard?

查看:130
本文介绍了如何将txt文件中的内容复制到剪贴板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的桌面上有一个txt文件:test.txt。我想打开该txt文件并将所有内容复制到剪贴板。

I have a txt file on my Desktop: test.txt. I want to open that txt file and copy everything to the clipboard.

我该怎么做?

我想出了如何打开文件并读取行:

I figured how to open file and read lines:

path = 'C:\Users\Username\Desktop\test.txt'

fo = open(path, 'r').readlines()

但是我不知道如何将这些数据放入剪贴板。

But I can't figure out how to get that data into the clipboard.

推荐答案

您可以尝试使用 Pyperclip

import pyperclip
fo = open(path, 'r').read()
pyperclip.copy(fo)

如果您使用的是OS X,还可以尝试以下代码:

If you're on OS X, you can also try this code:

import os 
data = "hello world"
os.system("echo '%s' | pbcopy" % data)

这篇关于如何将txt文件中的内容复制到剪贴板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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