Python Forum
How can I paste an entire file from clipboard to a folder? - 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: How can I paste an entire file from clipboard to a folder? (/thread-24315.html)



How can I paste an entire file from clipboard to a folder? - daverave1212 - Feb-08-2020

Hello,

I am making a script which works with non-text files.
I want to get the entire file, with name, extension, etc from the clipboard, and paste it at a given location.

Ex: User copies MyFile.dop, so now he has this file MyFile.dop in the clipboard. The script would get that file and paste it at C:\Program Files\here\.

How can I do that?
I haven't found anything useful on the internet so far.

Note: I am working on Windows 10.

Thanks!


RE: How can I paste an entire file from clipboard to a folder? - jim2007 - Feb-08-2020

Your ability to do something like this would be dependent on how good your knowledge of Windows system programming is.

Basically when you copy a file to the clipboard in Explorer, it puts the necessary information there in a certain format:

https://docs.microsoft.com/en-us/windows/win32/shell/clipboard#cfstr_filedescriptor

Once you have mastered the details of that format, the next step would be to figure out how to access the clipboard data in Python and decode it.


RE: How can I paste an entire file from clipboard to a folder? - daverave1212 - Feb-08-2020

(Feb-08-2020, 11:51 AM)jim2007 Wrote: Your ability to do something like this would be dependent on how good your knowledge of Windows system programming is.

Basically when you copy a file to the clipboard in Explorer, it puts the necessary information there in a certain format:

https://docs.microsoft.com/en-us/windows/win32/shell/clipboard#cfstr_filedescriptor

Once you have mastered the details of that format, the next step would be to figure out how to access the clipboard data in Python and decode it.

Hello, Tim! Thanks for the answer, it found something there.

I managed to find this way of doing it with win32clipboard:

import win32clipboard as cb

cb.OpenClipboard()

if cb.IsClipboardFormatAvailable(cb.CH_HDROP):
  clipboard_file_path = cb.GetClipboardData(cb.CF_HDROP)
  print(clipboard_file_path)

cb.CloseClipboard()
CF_HDROP refers to clipboard data about a copied file. It will raise an exception if the format is not CF_HDROP, so you can check if it's ok with cb.IsClipboardFormatAvailable.

Thanks!


RE: How can I paste an entire file from clipboard to a folder? - DeaD_EyE - Feb-08-2020

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')
If win32clipboard has a better support for clipboard, you should use this.
Tkinter is very limited.


RE: How can I paste an entire file from clipboard to a folder? - daverave1212 - Feb-08-2020

(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')
If win32clipboard 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!


RE: How can I paste an entire file from clipboard to a folder? - snippsat - Feb-08-2020

(Feb-08-2020, 03:56 PM)daverave1212 Wrote: I can't believe I couldn't find this stuff on the internet more easily.
Search PyPi to look if other has done something similar.
jaraco.clipboard can copy image.
He using a other main library he made,there can look at code.
open clipboard he use ctypes and do a direct call with to dll files with:
handle_nonzero_success(windll.user32.OpenClipboard(owner))


def get_image():
    with context():
        return GetClipboardData(clipboard.CF_DIB)