[Python] Is there a Python function to copy a file to the clipboard?

Discussão em 'Python' iniciado por Stack, Junho 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to automate the process of sending a large volume of emails, but our organization's email addresses do not support sending emails via SMTP because we use a single sign on and not a password. These emails each need to contain a different PDF attachment. Outlook supports pasting attachments into the body of the email to include them as an attachment, so all I really need is a way to store a specific PDF on the clipboard so I can paste it into the body of the email.

    From what I could find it seems like when you right click on a file in File Explorer and click copy, Windows isn't storing that entire file on the clipboard, it's storing some sort of reference to the file path, and pasting that path in Outlook results in Outlook interpreting that clipboard data as a command to attach the file found at that path. Not sure if that's a correct characterization of how the clipboard works, but that's what I took away from the other sources I found.

    I tried one solution I found online which seems to use win32clipboard to set the clipboard to a file path:

    import win32clipboard, os
    path = 'c:\\Users\\myusername\\MyPythonScripts\\filename.pdf'
    command = f"powershell Set-Clipboard -LiteralPath {path}"
    os.system(command)


    This returns "1", and when I try to paste the file there seems to be nothing in the clipboard. I expected the file to have been copied to the clipboard.

    Continue reading...

Compartilhe esta Página