Python Forum
I can't read data from the clipboard in an external program - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: I can't read data from the clipboard in an external program (/thread-16887.html)



I can't read data from the clipboard in an external program - AlekseyPython - Mar-19-2019

Kubuntu 18.10x64, Python 3.7.2

I used two ways to add text to the clipboard and in both of them the data can be read from the clipboard only within the Python program. It is impossible to insert data into an external text- file using the 'ctrl + v' combination or the mouse menu.

import tkinter
c = tkinter.Tk()
c.withdraw()
c.clipboard_clear()
c.clipboard_append('sample text')
#text_value = c.clipboard_get()
from PyQt5 import QtWidgets,QtCore
from PyQt5.QtGui import QClipboard
q_app = QtWidgets.QApplication(sys.argv)
clipboard = q_app.clipboard()
               
text_function = 'sample text'
clipboard.clear(mode=QClipboard.Clipboard)
clipboard.setText(text_function,  mode=QClipboard.Clipboard)

#added on the advice from here: https://stackoverflow.com/questions/1073550/pyqt-clipboard-doesnt-copy-to-system-clipboard
event = QtCore.QEvent(QtCore.QEvent.Clipboard)
q_app.sendEvent(clipboard, event)

#text_value = clipboard.text(mode=QClipboard.Clipboard)
How can I read data from the clipboard in an external program?

The pyperclip module works well, but requires the installation of additional libraries for Linux (which makes it difficult to migrate / reinstall the system): GitHub

import pyperclip
pyperclip.copy('The text to be copied to the clipboard.')



RE: I can read data from the clipboard in an external program - buran - Mar-19-2019

I would definitely go with pyperclip. Don't remember any issues with installing on linux mint
Even if some dependencies need to be installed as long as it is clearly stated in the documentation of your product there should be no problems