[Tutor] pandas.to_clipboard() copies nothing

Jim jf_byrnes at comcast.net
Sat Jun 6 23:44:05 EDT 2020


On 6/6/20 1:39 PM, Peter Otten wrote:

Peter,

I see that my original message never showed up so I am re-sending it.

 > Jim wrote:
 >
 >> On 6/6/20 4:25 AM, Peter Otten wrote:
 >>> Jim wrote:
 >>>
 >>>> Mint18.3, python3.6, pandas 0.24.1
 >>>>
 >>>>
 >>>> et_file = '/home/jfb/Downloads/PortfolioDownload.csv'
 >>>> df = pd.read_csv(et_file, header=6, usecols=[0, 7], skipfooter=6,
 >>>> engine='python')
 >>>> df.to_clipboard(sep=',')
 >>>> print(df)
 >>>>
 >>>> Manual notes the following. I'm not using PyQt4, but xclip is 
installed.
 >>>>
 >>>> Notes: Requirements for your platform.
 >>>>
 >>>>        Linux : xclip, or xsel (with PyQt4 modules)
 >>>>        Windows : none
 >>>>        OS X : none
 >>>>
 >>>> The print command prints what I expect to see.
 >>>>
 >>>> I don't use pandas that much so maybe I am missing something.
 >>>
 >>> The pandas side seems OK; maybe you are looking at the wrong selection?
 >>> I have a slightly different setup, and the following works for me:
 >>>
 >>> $ python3
 >>> Python 3.4.3 (default, Nov 12 2018, 22:25:49)
 >>> [GCC 4.8.4] on linux
 >>> Type "help", "copyright", "credits" or "license" for more information.
 >>>>>> import pandas
 >>>>>> df = pandas.DataFrame([[1,2], [3, 4]], columns=["Ham", "Spam"])
 >>>>>> df.to_clipboard(sep=", ")
 >>>>>>
 >>> $ xclip -o -selection clipboard
 >>>      Ham  Spam
 >>> 0    1     2
 >>> 1    3     4$
 >>>
 >>
 >> Peter,
 >>
 >> I'm not sure what you mean by the "wrong selection" above. I ran your
 >> code and got the following results.
 >>
 >>
 >> This version (space after ,) worked despite the error msg:
 >>
 >> Python 3.6.10 (default, Dec 19 2019, 23:04:32)
 >> [GCC 5.4.0 20160609] on linux
 >> Type "help", "copyright", "credits" or "license" for more information.
 >>   >>> import pandas
 >>   >>> df = pandas.DataFrame([[1,2,], [3,3]], columns=["Ham", "Spam"])
 >>   >>> df.to_clipboard(sep=", ")
 >> QApplication: invalid style override passed, ignoring it.
 >>       Available styles: Windows, Fusion
 >> 
/home/jfb/EVs/env36/lib/python3.6/site-packages/pandas/io/clipboards.py:134:
 >> UserWarning: to_clipboard in excel mode requires a single character
 >> separator.
 >>     warnings.warn('to_clipboard in excel mode requires a single '
 >>
 >> Copied from clipboard:
 >>      Ham  Spam
 >> 0    1     2
 >> 1    3     3
 >>
 >> This version (no space after ,) also worked despite a different 
error msg:
 >>
 >> (env36) jfb at jims-mint18 ~ $ python
 >> Python 3.6.10 (default, Dec 19 2019, 23:04:32)
 >> [GCC 5.4.0 20160609] on linux
 >> Type "help", "copyright", "credits" or "license" for more information.
 >>   >>> import pandas
 >>   >>> df = pandas.DataFrame([[1,2], [3,4]], columns=["Ham", "Spam"])
 >>   >>> df.to_clipboard(sep=",")
 >> QApplication: invalid style override passed, ignoring it.
 >>       Available styles: Windows, Fusion
 >>
 >> Copied from clipboard:
 >> ,Ham,Spam
 >> 0,1,2
 >> 1,3,4
 >>
 >>
 >> I had a little test file I was using to figure out pandas so I dropped
 >> your code in it. Nothing was copied to the clipboard and this is the
 >> error msg:
 >>
 >> (env36) jfb at jims-mint18 ~ $ python /home/jfb/Dev/Python/test_csv_file.py
 >> QApplication: invalid style override passed, ignoring it.
 >>       Available styles: Windows, Fusion
 >> 
/home/jfb/EVs/env36/lib/python3.6/site-packages/pandas/io/clipboards.py:134:
 >> UserWarning: to_clipboard in excel mode requires a single character
 >> separator.
 >>     warnings.warn('to_clipboard in excel mode requires a single '
 >>
 >> So I added excel=False and dropped the sep= and got this error:
 >>
 >> (env36) jfb at jims-mint18 ~ $ python /home/jfb/Dev/Python/test_csv_file.py
 >> QApplication: invalid style override passed, ignoring it.
 >>       Available styles: Windows, Fusion
 >>
 >> I don't understand the QApplication reference. I assume it refers to QT
 >> I have some QT stuff installed:
 >>
 >> from pip list:
 >> PyQt5                    5.12
 >> PyQt5-sip                4.19.14
 >> qtconsole                4.4.3
 >>
 >> I don't know why QT would come into play here and I don't understand the
 >> reference to excel when it was not included.
 >>
 >> To sum up. If I run it from the python console it give an error, but
 >> copies to the clipboard. If I run the python file from the terminal it
 >> gives an error and does not copy to the clipboard.
 >
 > Most of your errors seem to be warnings.
 >
 > Looking into the code writing to the clipboard shows that it requires the
 > DISPLAY environment variable to be set, so I'd check that in the 
terminal.
 >
 > 
https://github.com/pandas-dev/pandas/blob/master/pandas/io/clipboard/__init__.py#L539
 >
 > shows that
 >
 >      if HAS_DISPLAY:
 >          if _executable_exists("xsel"):
 >              return init_xsel_clipboard()
 >          if _executable_exists("xclip"):
 >              return init_xclip_clipboard()
 >          if _executable_exists("klipper") and 
_executable_exists("qdbus"):
 >              return init_klipper_clipboard()
 >          ... [try qt]
 >
 > QT is the last option. From that we can conclude that xclip is not found.
 > Maybe you can fix that by adjusting the path.
 > Or you try to set the clipboard mechanism explicitly with
 >
 > pandas.io.clipboard.set_clipboard("xclip")
 >

Well I have it working. I have a VE with python 3.5 in it, no QT 
installed. I ran the test script and it copied the info to the clipboard.

I went back to the VE with 3.6 and upgraded PyQt5 & PyQt5-sip. I ran the 
test script and it gave a Qt error basically saying it could not load a 
plugin even though it was installed. At that point I uninstalled the Qt 
stuff from the 3.6 VE and the script copied the dataframe to the 
clipboard. Then I tried your xclip command.

  xclip -o -selection clipboard
,Ham,Spam
0,1,2
1,3,4

Here is the output of items containing QT or DISPLAY from printenv:

QT_STYLE_OVERRIDE=
QT_LINUX_ACCESSIBILITY_ALWAYS_ON=1
QT_ACCESSIBILITY=1
QT_QPA_PLATFORMTHEME=qgnomeplatform
QT4_IM_MODULE=xim
DISPLAY=:0

You certainly know more about this than I do, but I wonder about the 
conclusion that xclip is not found. In VE 3.5 and now VE 3.6 there is no 
QT to fall back on but the script worked and xclip worked after the 
script ran. I don't know what DISPLAY should equal.

I don't think I need QT any more so I am going to stop trying to figure 
this out and try to finish the script I was working on. I do plan on 
coming back and see if there is a different solution other than deleting 
PyQt5.

I do want to thank you for all the help you have given me in this matter.

Regards,  Jim



More information about the Tutor mailing list