如何将多行复制到剪贴板?

4 投票
1 回答
2561 浏览
提问于 2025-04-18 14:13

有没有办法把多行文本复制到剪贴板上?

我试过以下命令,但对我来说没用:

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

我想要的输出是

sample one

sample two

sample three

1 个回答

4

使用剪贴板模块

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.

撰写回答