user3863344
user3863344

Reputation: 49

How to pass multiple lines to clipboard?

Is there a way to send multiple lines of text to the clipboard?

I've used following commands but didn't work for me:

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

I want o/p as

sample one

sample two

sample three

Upvotes: 4

Views: 2726

Answers (1)

hd1
hd1

Reputation: 34657

Use the 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

Right, so @Reman said the clipboard copy command overrides it, instead of appending. So, let's do the appending ourselves.

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

Upvotes: 4

Related Questions