Advertisement
steve-shambles-2109

Python Auto Clipboard Saver

Mar 10th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. """
  2. Auto Clipboard Saver
  3. Windows only. Tested on Windows 7.
  4.  
  5. By Steve Shambles. March 2020
  6. work in progress
  7.  
  8. pip install Pillow
  9. pip install pyperclip
  10.  
  11. More incompetent code like this at:
  12. stevepython.wordpress.com
  13. """
  14. from datetime import datetime
  15. import os
  16. from threading import Timer
  17. import winsound
  18.  
  19. from PIL import ImageGrab
  20. import pyperclip
  21.  
  22. # Check image folder exists in current directory.
  23. img_folder = ('clipboard_images')
  24. check_img_folder = os.path.isdir(img_folder)
  25.  
  26. #if it doesn't exist, then create it.
  27. if not check_img_folder:
  28.     os.makedirs(img_folder)
  29.     print('created folder : ', img_folder)
  30.  
  31. # Check text folder exists in current directory.
  32. txt_folder = ('clipboard_texts')
  33. check_txt_folder = os.path.isdir(txt_folder)
  34.  
  35. #if it doesn't exist, then create it.
  36. if not check_txt_folder:
  37.     os.makedirs(txt_folder)
  38.     print('created folder : ', txt_folder)
  39.  
  40. def beep_sound():
  41.     """Beep sound. frequency, duration."""
  42.     winsound.Beep(840, 100)
  43.  
  44. def save_cb_text():
  45.     """If text found on clipboard then Save to uniquely named text file."""
  46.     # Grab clipboard contents.
  47.     cb_txt = pyperclip.paste()
  48.  
  49.     if cb_txt:
  50.         tits = '-' *34+'\n'
  51.         time_stamp = (datetime.now().strftime(r'%y%m%d%H%M%S'))
  52.         file_name = time_stamp+str('.txt')
  53.         folder = 'clipboard_texts\\'
  54.  
  55.         with open(folder+str(file_name), 'w') as contents:
  56.             contents.write('Clipboard text found: '+str(time_stamp)+'\n')
  57.             contents.write(tits)
  58.             contents.write(cb_txt)
  59.             beep_sound()
  60.  
  61.             print('Clipboard text found and saved as', file_name)
  62.             # Clear clipboard.
  63.             pyperclip.copy('')
  64.  
  65. def grab_cb_img():
  66.     """If image found on clipboard, Save to uniquely named jpg file."""
  67.     # Grab clipboard.
  68.     img = ImageGrab.grabclipboard()
  69.  
  70.     # If an image is found on the clipboard...
  71.     if img:
  72.         # Create unique file name using current full date and time.
  73.         file_name = (datetime.now().strftime(r'%y%m%d%H%M%S'))+'.jpg'
  74.         folder = 'clipboard_images\\'
  75.         img.save(folder+str(file_name))
  76.         beep_sound()
  77.  
  78.         print('Clipboard image found and saved as', file_name)
  79.         # Clear clipboard so that same image is not saved again.
  80.         pyperclip.copy('')
  81.  
  82. def grab_img():
  83.     """Main loop using 1 second timer to check clipboard."""
  84.     grab_cb_img()
  85.     save_cb_text()
  86.  
  87.     t = Timer(1, grab_img)
  88.     t.start()
  89.  
  90. grab_img()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement