Last active
September 18, 2023 16:10
-
-
Save Erriez/8d0f9d0855708da7fd85c45e6e9a62f6 to your computer and use it in GitHub Desktop.
Python example to load/save clipboard formats from/to a file on Windows using win32clipboard pywin32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# MIT License | |
# | |
# Copyright (c) 2023 Erriez | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
import argparse | |
import os | |
import sys | |
import win32clipboard # pip install pywin32==306 | |
import _pickle as cPickle | |
from pprint import pprint | |
DESCRIPTION = 'Python example to load/save clipboard formats from/to a file on Windows.' | |
VERSION = '1.0.0' | |
def get_clipboard(): | |
clipboard_dict = {} | |
# Open clipboard | |
win32clipboard.OpenClipboard() | |
# Create list clipboard formats | |
cf = [win32clipboard.EnumClipboardFormats(0)] | |
while cf[-1]: | |
cf.append(win32clipboard.EnumClipboardFormats(cf[-1])) | |
cf.pop() | |
# Get clipboard format name and data | |
for format_id in cf: | |
try: | |
format_name = win32clipboard.GetClipboardFormatName(format_id) | |
except: | |
format_name = "" | |
try: | |
format_data = win32clipboard.GetClipboardData(format_id) | |
except: | |
format_data = None | |
clipboard_dict[format_id] = {'name': format_name, 'data': format_data} | |
# Close clipboard | |
win32clipboard.CloseClipboard() | |
return clipboard_dict | |
def set_clipboard(clipboard_dict): | |
# Open and empty clipboard | |
win32clipboard.OpenClipboard() | |
win32clipboard.EmptyClipboard() | |
# Set clipboard data per clipboard format | |
for format_id in clipboard_dict: | |
try: | |
format_data = clipboard_dict.get(format_id).get('data') | |
if format_data: | |
win32clipboard.SetClipboardData(format_id, format_data) | |
except: | |
print('Error writing format ID {}'.format(format_id)) | |
# Close clipboard | |
win32clipboard.CloseClipboard() | |
def clipboard_save(clipboard_file, verbose=False): | |
clipboard_dict = get_clipboard() | |
if verbose: | |
pprint(clipboard_dict) | |
try: | |
with open(clipboard_file, 'wb') as f: | |
cPickle.dump(clipboard_dict, f) | |
except OSError as e: | |
print(e) | |
def clipboard_load(clipboard_file, verbose=False): | |
if not os.path.exists(clipboard_file): | |
print(f'Error: {clipboard_file} not found') | |
return | |
try: | |
with open(clipboard_file, 'rb') as f: | |
clipboard_dict = cPickle.load(f) | |
except OSError as e: | |
print(e) | |
return | |
if verbose: | |
pprint(clipboard_dict) | |
set_clipboard(clipboard_dict) | |
def main(): | |
# Check platform | |
if sys.platform != 'win32': | |
print(f'Unsupported platform {sys.platform}') | |
sys.exit(1) | |
# Argument parser | |
parser = argparse.ArgumentParser(description=f'{DESCRIPTION} v{VERSION}') | |
parser.add_argument('-i', '--input-file', help='Clipboard input file') | |
parser.add_argument('-o', '--output-file', help='Clipboard output file') | |
parser.add_argument('-v', '--verbose', | |
action='store_true', | |
default=False, | |
help='Print verbose') | |
args = parser.parse_args() | |
# Clipboard example | |
if not args.output_file and not args.input_file: | |
clipboard_file = os.path.join(os.path.expanduser('~'), 'clipboard.bin') | |
clipboard_save(clipboard_file, args.verbose) | |
clipboard_load(clipboard_file, args.verbose) | |
else: | |
if args.output_file: | |
clipboard_save(args.output_file, args.verbose) | |
if args.input_file: | |
clipboard_load(args.input_file, args.verbose) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment