Search code examples
pythonnewlineclipboardpyperclip

Copy text with newlines to clipboard :Python


I want to copy text with newlines to the clipboard using the following code.

import pyperclip
numbers=''
for i in range(200):
    numbers = numbers + str(i) + '\n'
pyperclip.copy(numbers)

After execution the clipboard should contain:

0
1
2
.
.
200

But after I run the program and paste in Notepad. I see

012345....200

All in a single line. :( I use Python 3.6.1 on Windows 10


Solution

  • I assume you're pasting into microsoft notepad. In this case you should use \r\n (windows style) instead of \n only (unix style)

    If you paste into notepad++ it will treat the newline character unix style, and you will see it separated like you want.

    If you want to be OS independent then use os.linesep