如何将多行传递给剪贴板?

时间:2014-07-22 06:34:48

标签: 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

1 个答案:

答案 0 :(得分:4)

使用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.