Python Forum
How to copy data to clipboard
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to copy data to clipboard
#1
I am trying to copy data from my Python program to the clipboard so that I can paste it into a spreadsheet for graphing and display.
I tried using pyperclip, but this only works with simple data types like strings. My data consists of tables (ie lists of lists) which can be exported as .csv files.

My next attempt was to use the bash command xsel
xsel –clipboard<temp.csv
which when run from the command line works exactly as I expect – using Ctrl-V inside Calc pastes my file into the spreadsheet.

But when the folllowing code is run from Python, Ctrl-V does nothing, though it returns a success error code
#! /usr/bin/env python3.6
#
# xsel test program

from subprocess import run
err = run(["xsel", "--clipboard", "<", "temp.csv"], shell=True)
print(err)
The output is
Error:
CompletedProcess(args=['xsel', '--clipboard', '<', 'temp.csv'], returncode=0)
(The absence/presence of spaces round the < make no difference to either outcome)

Any idea what I am doing wrong?
Python 3.6.9, Ubuntu 18.04.2 LTS
Reply
#2
look at pyperclip

you can use it. On Linux it's a wrapper around xclip or xsel (https://pypi.org/project/pyperclip/)
or you can look at the implementation:
xsel: https://github.com/asweigart/pyperclip/b...__.py#L222
xclip: https://github.com/asweigart/pyperclip/b...__.py#L194
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I tried pyperclip, but it doesn't seem to handle lists. See line 103 of your xsel reference.
Reply
#4
Is there a reason you want to copy and paste the data into a spreadsheet instead of using Python libraries for your analysis and plotting (matplotlib, pandas, ...)? Note in particular that pandas has facilities for creating Excel spreadsheets if you needed.
Reply
#5
I agree with @ndc85430 look into tools mention.
They have a lot power,Pandas can also copy from clipboard with pd.read_clipboard
So if a copy .csv file under from clipboard into a JupyterLab Notebook,then can do plot and other stuff quite easy.
Output:
Users,date,count daily_users,19-03-2017,219 daily_users,19-03-2018,5040 daily_users,19-03-2019,13579 weekly_users,19-03-2017,1767 weekly_users,19-03-2018,26664 weekly_users,19-03-2019,72166
Then look like this in Notebook and doing a plot with Altair.
[Image: H7Lsjd.png]
Reply
#6
In response to buran -
Next week I will look at the pyperclip code & see if I can construct a less restrictive wrapper for xsel from it.

In response to ndc85430-
I am building a database using Python to code the databsse engine and Calc to to the reports. Both bits are working, but the link between them is manual & tedious; using the clipboard would greatly improve matters.

I could of course learn Panda and use it to rebuild the back end. But that is not high up my list of prorities; much more urgent is to learn Tkinter and build a front end user interface.

I had hoped that using xsel would be quick and straightforward, but apparently not.
Reply
#7
I am bit confused...

(Mar-04-2020, 04:10 PM)OAP Wrote: I tried pyperclip, but it doesn't seem to handle lists.
Why do you need it to handle lists? read the whole file as text and put it in the clipboard with pyperclip. Isn't it the same what xsel would do when executed via terminal?
(Mar-05-2020, 03:52 PM)OAP Wrote: I am building a database using Python to code the databsse engine and Calc to to the reports. Both bits are working, but the link between them is manual & tedious; using the clipboard would greatly improve matters.

I could of course learn Panda and use it to rebuild the back end.
So you read from DB and save the result as text file, then you look for a way to transfer this text file via clipboard into a Calc/excel file. To me it would have been much easier to just create the calc/excel file without the intermediate text file and the need to use clipboard. You don't need to really learn pandas for that. And in addition to pandas, you can use number of other packages to work with excel or open office file formats.
Refactoring that part would be way simpler than trying to write less restrictive wrapper around xsel.

If you are going to learn GUI framework, look at wxPython Phoenix, instead of Tkinter. Or some other framework (PyQt/PySide, Kivy, pyGTK), . In long term that would be more beneficial IMHO.

https://wiki.python.org/moin/GuiProgramming
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
Can also mention that your subprocess code is wrong.
When commands in a list call(recommended),then can not use shell=True.
Also use capture_output=True to catch stdout from xsel.
from subprocess import run

err = run(['xsel', '--clipboard'], capture_output=True, encoding='utf-8')
print(err.stdout)
Output:
hello this text is from clipboard
An other library is jaraco.clipboard which can do html/image format to.
As @buran mention so may there be other way to do this,that may be just easy as copy content from clipboard.
Reply
#9
To buran-
Let me try to clarify the process.
I create a table (in Python terms, a list of lists) in my DB code. I try to copy this table to the clipboard using pyperclip, then manually paste it into a preformatted Calc template which I can open from within my software. But pyperclip baulks at handling lists, so that approach fails.
The next approach is to save the table as a .csv file, and get xsel to copy that file to the clipboard. Once again the preformatted template is open and ready to manually paste the data into.
What I am currently doing, which is manual and tedious, is to open the .csv file, and cut and paste it to the template.

To snippsat -
Thank you for that suggestion. I shall not get a chance to try it till next week, but I will let you know how I get on.

To everybody -
Thanks for being so helpful.
Reply
#10
(Mar-05-2020, 09:14 PM)OAP Wrote: Let me try to clarify the process.
I understood the process perfectly fine from the first time. However your question is perfect example of so called XY problem.
The problem with your approach is you try to put a list of lists in the pyperclip. So you save to csv file, which you try to read into clipboard. Instead of writting to csv file, you can just create single string from the list of list and put that str into clipboard (achieving the same result what you will get by reading the csv in the clipboard).

Now, as we try to explain to you using clipboard is unnecessary complication.

Using your current code to read from DB which gives you list of lists, you can then use this list of lists to write directly to excel/calc file.
one library for working with ods files is pexcel-ods3: https://github.com/pyexcel/pyexcel-ods3
you can also use openpyxl to work with xslx file

you can also use pandas for reading from db and/or writing to excel/calc file
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 413 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  How to copy work sheet data one workbook to other? sayyedkamran 2 811 Nov-03-2023, 09:10 AM
Last Post: Larz60+
  non-latin characters in console from clipboard Johanson 3 832 Oct-26-2023, 10:10 PM
Last Post: deanhystad
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 2,351 Jun-06-2023, 05:49 PM
Last Post: rajeshgk
  saving and loading text from the clipboard with python program MaartenRo 2 1,819 Jan-22-2022, 05:04 AM
Last Post: MaartenRo
  How to listen to clipboard content change without polling the clipboard continuously? edgelord 0 2,585 Nov-27-2020, 06:07 AM
Last Post: edgelord
  Problem posting image to clipboard noel 0 2,271 Sep-26-2020, 10:50 AM
Last Post: noel
  How can I copy and paste data from text file into an Excel sheet? IrishOrigi 14 7,565 Mar-19-2020, 07:25 PM
Last Post: Larz60+
  How can I paste an entire file from clipboard to a folder? daverave1212 5 9,018 Feb-08-2020, 04:33 PM
Last Post: snippsat
  Copy and append data from .dat file Makada 17 8,544 Jan-20-2020, 08:33 PM
Last Post: Makada

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020