Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Auto Clipboard Saver
- Windows only. Tested on Windows 7.
- By Steve Shambles. March 2020
- work in progress
- pip install Pillow
- pip install pyperclip
- More incompetent code like this at:
- stevepython.wordpress.com
- """
- from datetime import datetime
- import os
- from threading import Timer
- import winsound
- from PIL import ImageGrab
- import pyperclip
- # Check image folder exists in current directory.
- img_folder = ('clipboard_images')
- check_img_folder = os.path.isdir(img_folder)
- #if it doesn't exist, then create it.
- if not check_img_folder:
- os.makedirs(img_folder)
- print('created folder : ', img_folder)
- # Check text folder exists in current directory.
- txt_folder = ('clipboard_texts')
- check_txt_folder = os.path.isdir(txt_folder)
- #if it doesn't exist, then create it.
- if not check_txt_folder:
- os.makedirs(txt_folder)
- print('created folder : ', txt_folder)
- def beep_sound():
- """Beep sound. frequency, duration."""
- winsound.Beep(840, 100)
- def save_cb_text():
- """If text found on clipboard then Save to uniquely named text file."""
- # Grab clipboard contents.
- cb_txt = pyperclip.paste()
- if cb_txt:
- tits = '-' *34+'\n'
- time_stamp = (datetime.now().strftime(r'%y%m%d%H%M%S'))
- file_name = time_stamp+str('.txt')
- folder = 'clipboard_texts\\'
- with open(folder+str(file_name), 'w') as contents:
- contents.write('Clipboard text found: '+str(time_stamp)+'\n')
- contents.write(tits)
- contents.write(cb_txt)
- beep_sound()
- print('Clipboard text found and saved as', file_name)
- # Clear clipboard.
- pyperclip.copy('')
- def grab_cb_img():
- """If image found on clipboard, Save to uniquely named jpg file."""
- # Grab clipboard.
- img = ImageGrab.grabclipboard()
- # If an image is found on the clipboard...
- if img:
- # Create unique file name using current full date and time.
- file_name = (datetime.now().strftime(r'%y%m%d%H%M%S'))+'.jpg'
- folder = 'clipboard_images\\'
- img.save(folder+str(file_name))
- beep_sound()
- print('Clipboard image found and saved as', file_name)
- # Clear clipboard so that same image is not saved again.
- pyperclip.copy('')
- def grab_img():
- """Main loop using 1 second timer to check clipboard."""
- grab_cb_img()
- save_cb_text()
- t = Timer(1, grab_img)
- t.start()
- grab_img()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement