Feb-08-2020, 03:56 PM
(Feb-08-2020, 01:03 PM)DeaD_EyE Wrote: You can copy text from clipboard with Tkinter or Pyperclip.
Tkinter is shipped together with Python.
What you could not do, is getting raw bytes from a copied image to clipboard.
Tkinter doesn't seem to support it.
If you copy a File to your Clipbaord, you'll get a String back.
This code should work and return a Path object, if the Path was valid.
Otherwise it return None.
from pathlib import Path from tkinter import Tk, TclError def get_clipboard_as_path(): root = Tk() root.withdraw() try: content = root.selection_get(selection="CLIPBOARD") except TclError: return None finally: root.destroy() file = Path(content) try: if file.exists(): return file except OSError: pass return None # get the path my_path = get_clipboard_as_path() # my_path could be None if my_path: print(f'The path is {my_path}') # if my_path is a directory, find all python files if my_path.is_dir(): print('The path is a directory') for file in my_path.glob('*.py'): print(file) else: print('The path is a file') with my_path.open() as fd: header = fd.readline() print('First line:') print(header) else: print('It was not a Path, maybe a image or something else')Ifwin32clipboard
has a better support for clipboard, you should use this.
Tkinter is very limited.
Nice, thanks for the suggestions!
I can't believe I couldn't find this stuff on the internet more easily.
Cheers!