0

I am trying to use PyClip to copy and paste into and form clipboard.

These work correctly:

import pyclip
pyclip.copy("ab")
print(list(pyclip.paste()))

returns

[97, 98]
import pyclip
pyclip.copy("ab")
print(pyclip.paste(text=True))

returns

ab

But now I want to copy to clipboard "ab" but in the form of bytes:

import pyclip
pyclip.copy(bytes(97))
print(pyclip.paste(text=True))

returns some garbage

So how to copy into clipboard bytes 97 and 98 that when pasted somewhere else I would got "ab"?

Update:

To be more precise. I want to copy a string into clipboard in the form of bytes by Python and then by pressing CTRL+V in windows I want the same string to be pasted somewhere.

azerbajdzan
  • 149
  • 8

1 Answers1

1

repl help of bytes:

class bytes(object)
 |  bytes(iterable_of_ints) -> bytes
 |  bytes(string, encoding[, errors]) -> bytes
 |  bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
 |  bytes(int) -> bytes object of size given by the parameter initialized with null bytes
 |  bytes() -> empty bytes object

So, my guess is that when you do pyclip.copy(bytes(97)) .. that maps to bytes(int) so, you get bytes object with null bytes and its size is 97, eg:

>>> len(bytes(97))
97
>>>

Maybe you want something like:

pyclip.copy(bytes([97, 98]))
rasjani
  • 7,372
  • 4
  • 22
  • 35
  • But why it adds the "b" before it? It returns "b'ab'" when pasted back by python. Also when I try to paste into, say google search bar in my web browser by pressing CTRL+V I got nothing. I would expect that "ab" should be pasted. – azerbajdzan Aug 25 '22 at 09:54
  • @azerbajdzan https://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal – rasjani Aug 25 '22 at 09:56
  • I updated my question. I want the "copy into clipboard done by Python" to be available inside windows. – azerbajdzan Aug 25 '22 at 10:00
  • @azerbajdzan It should work, the example you gave was just wrong because of wrong usage of `bytes()` - you where passing a *single int*, not iterable. – rasjani Aug 25 '22 at 10:05
  • I agree, it should, but it does not. I typed exactly as you wrote `pyclip.copy(bytes([97, 98]))` but when I press `CTRL+V` it does not paste anything like if clipboard was empty. – azerbajdzan Aug 25 '22 at 10:08
  • On the other hand when I use `pyclip.copy("ab")` and then press `CTRL+V` in windows I got pasted `ab` as required. – azerbajdzan Aug 25 '22 at 10:12
  • Seems to be windows related then as it works straight up like that in Mac. Maybe you could add the encoding argument to `pyclip.copy()` – rasjani Aug 25 '22 at 10:14
  • I tried all different encodings, none of them work. Is there any other way to work with clipboard than using `PyClip`? – azerbajdzan Aug 25 '22 at 10:24