Python Forum
How can I paste an entire file from clipboard to a folder?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I paste an entire file from clipboard to a folder?
#1
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!
Reply
#2
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...descriptor

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.
There is no passion to be found playing small - in settling for a life that is less than the one you are capable of living.
Reply
#3
(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...descriptor

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!
Reply
#4
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(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!
Reply
#6
(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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy Paste excel files based on the first letters of the file name Viento 2 348 Feb-07-2024, 12:24 PM
Last Post: Viento
  Make entire script run again every 45 mo NDillard 0 293 Jan-23-2024, 09:40 PM
Last Post: NDillard
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 466 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  What script to paste folders thenewcoder 1 639 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  non-latin characters in console from clipboard Johanson 3 656 Oct-26-2023, 10:10 PM
Last Post: deanhystad
  Reading a file name fron a folder on my desktop Fiona 4 851 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Please help me [copy and paste file from src to dst] midomarc 2 963 Nov-24-2022, 10:13 PM
Last Post: midomarc
  Function not executing each file in folder mathew_31 9 2,170 Aug-22-2022, 08:40 PM
Last Post: deanhystad
  saving and loading text from the clipboard with python program MaartenRo 2 1,622 Jan-22-2022, 05:04 AM
Last Post: MaartenRo
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 5,879 Jan-07-2022, 04:55 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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