python - 如何将多行传递到剪贴板?

标签 python clipboard

有没有办法将多行文本发送到剪贴板?

我使用了以下命令但对我不起作用:

import os
text = """sample one \r\n sample two \r\n sample three"""
command = 'echo ' + text.strip() + '| clip'.
os.system(command)

我想要o/p作为

sample one

sample two

sample three

最佳答案

使用 clipboard module :

import clipboard
clipboard.copy("line1\nline2")  # now the clipboard content will be string "line1\nline2"
clipboard.copy("line3") # add line3
text = clipboard.paste()  # text will have the content of clipboard

是的,@Reman 说剪贴板复制命令会覆盖它,而不是追加。因此,让我们自己进行追加。

line = '\n'.join(line, new_line)
clipboard.copy(line)
text = clipboard.paste() # now all lines separated by newline will be on the clipboard.

关于python - 如何将多行传递到剪贴板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24880400/

相关文章:

Python - ConfigParser 抛出评论

python - Selenium |将多个扩展加载到 Selenium 的 ChromeDriver 中,只接收最后定义的

javascript - 使用 useRef Hook 将副本复制到剪贴板

windows-7 - 剪贴板复制和粘贴在 VMWare Player 12 中不起作用

python - 在 BeautifulSoup 中使用多个条件

控制台中出现 Python 错误,但文件 : unexpected character after line continuation character 中没有

python - TensorFlow 中出现错误

delphi - 从 Firefox 复制图像时出现黑色背景

c# - 在剪贴板更改时修改剪贴板内容

javascript - 如何使用 jQuery 将文本复制到客户端的剪贴板?