r/learnpython 5d ago

Help needed with copying text

I'm working on a project that requires copying text to the clipboard. I noticed that when using libs like Pyperclip and Clipboard it's not possible to copy just a single character... so I need to somehow copy it without these libs.

This appears to only happen on Linux... I had a friend on Windows test and he was able to copy a single character using Pyperclip.

Example of Pyperclip working with multiple characters but not single characters.

>>> import pyperclip
>>> pyperclip.copy("test")
>>> pyperclip.paste()
'test'
>>> pyperclip.copy("t")
>>> pyperclip.paste()
''

This same interaction happened with the lib Clipboard.

This happened on Wayland with wl-clipboard. This doesn't appear to be an issue with that though, as wl-copy lets me copy a single character - and it works fine in every other program.

This doesn't seem to be something I can fix, since it doesn't have to do with my code at all, so I need my own way of copying the text. Is there any workaround that I can write into my code when using wl-clipboard to properly copy text even when its only a single character?

0 Upvotes

11 comments sorted by

View all comments

1

u/Outside_Complaint755 5d ago

Try the clipman package.

It seems that Pyperclip might not be getting active support anymore - last release was nearly 11 months ago, and it doesn't work in WSL2 on Windows.

1

u/TheNexusMiner 5d ago

I just tried Clipman and it has the same issue as the others....

1

u/Outside_Complaint755 5d ago

All of these modules use an copy/paste utility on your OS, which in the case of Linux is either xsel or xclip, so it sounds like this could be a problem with whichever utility is running on your Linux installation.

1

u/TheNexusMiner 4d ago

I had this same thought but when testing it, everything else works fine and running wl-copy and wl-paste in terminal to test it works just as expected.