0

I just found out that for some reason when copying using pyperclip a string that was decoded (using utf-8), it will raise an error.

import pyperclip
with open('chat.txt' 'r') as f:
    string = f.read()
# the string is encoded in utf-8 in order to be able to write down `'`, `emoji` and other special signs or symbol
pyperclip.copy(string.decode('utf-8'))

It will raise this error: PyperclipException: only str, int, float, and bool values can be copied to the clipboard, not unicode

I found a roundabout way to solve it by using str() but then found out that it won't work since str() does not work if there are some character like '.


EDIT: Alternative solution

An alternative solution except for the solution that I accepted would be degrade the pyperclip from the newest version (right now its 1.6.4) to a lower version (1.6.1 worked for me).

Programer Beginner
  • 1,377
  • 6
  • 21
  • 47

2 Answers2

1

This issue has been fixed in 1.6.5, so all you have to do is update pyperclip by running pip install -U pyperclip.

Al Sweigart
  • 11,566
  • 10
  • 64
  • 92
0

You seem to be facing some issues with non-ASCII quotation marks. I suggest you to use Python 3.7. Here's a sample:

import pyperclip

with open('chat.txt', 'r') as f:
    string = f.read()
pyperclip.copy(string)

This is an alternative for Python 2.7:

import pyperclip
import sys
reload(sys)
sys.setdefaultencoding('utf8')

with open('chat.txt', 'r') as f:
    string = f.read()

pyperclip.copy(string)

Warning: As pointed out in a comment by @lenz the use of sys.setdefaultencoding() is a hack and discouraged for a number of reasons.

xilopaint
  • 699
  • 1
  • 7
  • 16
  • Well another issue is that I get the string from a file and I don't know where the ' is and whether other character will pose issue or not... – Programer Beginner Sep 06 '18 at 00:23
  • You need to provide more details on how you are retrieving the string from the file. – xilopaint Sep 06 '18 at 01:14
  • Your question isn't clear but I guess I could reproduce your issue. I've just edited the answer accordingly. – xilopaint Sep 06 '18 at 02:01
  • I just edit my code to be more clear but you pretty much reproduced my issue. Is there any other way than switching to python 3 because I would have to convert my whole python 2.7 code to python 3. – Programer Beginner Sep 06 '18 at 06:47
  • @ProgramerBeginner I suggest you upgrade to Python 3 anyway. You'll have to sooner or later. The `2to3` tool (part of the Python installation) does a very good job at helping you port the code. – lenz Sep 06 '18 at 07:39
  • @ProgramerBeginner I just edited the answer again with an alternative code for Python 2. Anyway, it should be a good opportunity to you move to Python 3. – xilopaint Sep 06 '18 at 07:42
  • @xilopaint Using `sys.setdefaultencoding` is a hack that shouldn't be used (that's why you have to `reload(sys)` to make it work). See [here](https://stackoverflow.com/questions/3828723/why-should-we-not-use-sys-setdefaultencodingutf-8-in-a-py-script) for some reasons why. – lenz Sep 07 '18 at 05:05
  • @ProgramerBeginner Does this answer address your question? – xilopaint Sep 12 '18 at 05:43
  • @xilopaint Oh sorry, I forgot to mark it as answered. Yes it kind of answer my questions, though I at the end choose to degrade pyperclip to version `1.6.1`, which for some reason worked for me... – Programer Beginner Sep 16 '18 at 11:20