如何在python中将文件复制到剪贴板

mgdq6dx1  于 2023-02-01  发布在  Python
关注(0)|答案(1)|浏览(289)

像往常一样,我们可以直接复制文件到剪贴板简单的ctrl + c
但我想用python来实现

path = "..."
def copy_file_to_clipboard() -> None: 
   clipboard.copy_file(path) # this is a false code, but I want to know the code to do so
wmvff8tz

wmvff8tz1#

下面是在python 3.9(Notebook jupyter)中测试的解决方案:

import pyperclip as pc
def copy_to_clipboard(file_name):
    with open(file_name, 'r') as f:
        text = f.read()
        pc.copy(text)
        print("File successfully copied to clipboard!")
copy_to_clipboard('file.txt')

相关问题