1

I am trying to find out an answer to the above question - I have already tried shutil, which to my knowledge, does not work because I do not want to copy the file to a destination.

Currently using Pyperclip module for text to the clipboard, but this does not work with actual local files.

My end goal is to copy two files to my clipboard using python code, then be able to paste them into an email.

10 Rep
  • 2,217
  • 7
  • 19
  • 33
MSawers
  • 11
  • 3
  • What is your Python code for composing email? – olha Jul 19 '20 at 20:50
  • I don't have python code for composing email just yet - I am still working on that. The program is copying text (and hopefully files) to the clipboard, then I go to outlook and paste it into an email. – MSawers Jul 19 '20 at 20:54
  • Open the file, read it, use Pyperclip to put the contents on the clipboard? – jasonharper Jul 19 '20 at 20:57
  • I'd advise you to start from the end: from composing email. You can use `mock`/`stub` for the __actual__ file contents, e.g. let the "copied" text always be "hello world". Then, you'll replace this "hello world" either with filepath / or with file contents / or with whatever your email composing code will require. – olha Jul 19 '20 at 20:58
  • Thanks for your reply Jason, but they are .pdf files that I need the recipient to be able to download themselves and print off if they would like to. – MSawers Jul 19 '20 at 20:59
  • Sorry olha I do not quite understand that. – MSawers Jul 19 '20 at 21:00
  • Is it necessary to use `python`? If you don't have any preprocessing code, merely copy, yoou can try `pbcopy` (on MacOS) to copy the content of file to clipboard: `pbcopy < [path to your file]`. Then, paste it anywhere you want with `Command + v`. You can see more [here](https://superuser.com/questions/298227/how-to-send-the-contents-of-a-text-file-to-the-clipboard-from-the-command-line-i) or [here](https://stackoverflow.com/questions/1753110/how-do-i-capture-bash-output-to-the-mac-os-x-clipboard). – Hoa Nguyen Jul 19 '20 at 21:05
  • That's interesting Hoa thank you - can I use pbcopy inside of IDLE? I am new to programming so I am unsure of the relationship between the terminal and programming environments. – MSawers Jul 19 '20 at 21:08
  • @MSawers you do it in the terminal. Just open the terminal, write the command: `pbcopy < [path to your file]. For example, I need to copy the content of file `data.csv` in my Desktop to clipboard, I will to this: `pbcopy ~/Desktop/data.csv`. Then, I paste the content anywhere I like by the shortcut: `Command + V`. – Hoa Nguyen Jul 19 '20 at 21:14
  • However, as you said, the files are in `pdf` format, I think you need some preprocessing to read the file. `pbcopy` simply copy text, not the format. – Hoa Nguyen Jul 19 '20 at 21:19

1 Answers1

2

I'm not sure if this will solve for pasting into an email, but I came across this question while looking for how to copy stuff to the clipboard in macOS and have since found a solution that works for me. I figured I'd post it in case anyone else happens across this post with similar needs.

As mentioned in this question's comments, I came across Pyperclip. However I don't want any external dependencies. So, I took a peak at how Pyperclip implemented copy/paste and it's relatively simple. See here.

The short of it is this:

#!/usr/bin/env python3

import subprocess

# copy
def pbcopy(txt):
    task = subprocess.Popen(
        ['pbcopy'],
        stdin=subprocess.PIPE,
        close_fds=True
    )
    task.communicate(input=txt.encode('utf-8'))

# paste
def pbpaste():
    task = subprocess.Popen(
        ['pbpaste'],
        stdout=subprocess.PIPE,
        close_fds=True
    )
    stdout, stderr = task.communicate()
    return(stdout.decode('utf-8'))

Hopefully this solves @MSawers question and if not hopefully someone else will find this useful.

bheinz
  • 1,230
  • 1
  • 10
  • 11