On Windows 10, under Anaconda, I've installed pyperclip so that I can copy content normally viewed
in the Spyder console: conda install -c conda-forge pyperclip. Wihtout this, lengthy output scrolls off the top of the console terminal and is lost because of the limited capture depth.
As a test of correct functionality, the following allows me to paste the copied string "dog" into a Cygwin xterm:
import pyperclip as pc
pc.copy("dog")
To meet pyperclip's input requirements, I convert the sys.modules dict into a
DataFrame DFstr, then export it to a string:
import pandas as pd
import sys
DFstr = pd.DataFrame( sys.modules.items(),
columns=["Local name", "Module"] ).to_string
DFstr # Show string content
<bound method DataFrame.to_string of Local name Module
0 sys <module 'sys' (built-in)>
1 builtins <module 'builtins' (built-in)>
2 _frozen_importlib <module 'importlib._bootstrap' (frozen)>
3 _imp <module '_imp' (built-in)>
4 _thread <module '_thread' (built-in)>
... ...
1758 bs4.builder <module 'bs4.builder' from 'C:\\Users\\User.Name...
1759 bs4 <module 'bs4' from 'C:\\Users\\User.Name\\anacon...
1760 zmq.utils.garbage <module 'zmq.utils.garbage' from 'C:\\Users\\U...
1761 pandas.io.formats.string <module 'pandas.io.formats.string' from 'C:\\U...
1762 pandas.io.formats.html <module 'pandas.io.formats.html' from 'C:\\Use...
[1763 rows x 2 columns]>
However, DFstr fails to copy to the pyperclip "clipboard":
pc.copy(DFstr)
Traceback (most recent call last):
Cell In[49], line 1
pc.copy(DFstr)
File
~\anaconda3\envs\py39\lib\site-packages\pyperclip\__init__.py:463
in copy_windows
text = _stringifyText(text) # Converts non-str values to str.
File
~\anaconda3\envs\py39\lib\site-packages\pyperclip\__init__.py:111
in _stringifyText
raise PyperclipException('only str, int, float, and bool values
can be copied to the clipboard, not %s' % (text.__class__.__name__))
PyperclipException:
only str, int, float, and bool values
can be copied to the clipboard, not method
What can I do to trace down the problem?
As per the error message,
DFstris a method, whichpyperclip.copy()will not accept. The reason why it is a method instead of a string is because it was assignedDataFrame.to_stringinstead ofDataFrame.to_string(). The former is a method, whereas the latter is an invocation of methodto_string(), which returns a string [1].When invoking a class/object method with zero arguments, some languages will allow you to dispense with the brackets
()for enclosing arguments, but not Python. It doesn't interpret bracket-less function names as invocations, but rather, as referring to the functions/methods themselves.Notes
[1] See
help(pd.DataFrame.to_string).