How to get Clipboard data?

How to get Clipboard data using Python ready-to-use function?

Install the following module(s) if you haven’t installed them already:

pip install pywin32

This Python function provides a convenient way to get the content of the clipboard. The clipboard content can be read as text or list items. The ready-to-use Python function to get Clipboard data is below:

def get_clipboard_text(option='text', list_option='\r\n'):
    # Returns the text currently stored in the clipboard.
    clipboard_text = win32clipboard.GetClipboardData()
    # clip to text or list
    if option == 'list':
        try: clipboard_data = clipboard_text.split(list_option)
        except Exception as err:
            print('Error Message: ' + str(err))
    elif option == 'text':
        clipboard_data = clipboard_text
        messagebox.showinfo('Message', 'Please provide correct option (text or list)!')
    # return clipboard data
    if clipboard_data[len(clipboard_data)-1] == '': clipboard_data.pop()
    return clipboard_data

Write your main code as a sample below,

import win32clipboard

print(get_clipboard_text('list', '\n'))
print(get_clipboard_text('list', ' '))

Copy the text below and then execute the sample code above,

base= 'Win32GUI'
The output of the code is (text),

base= 'Win32GUI'

The output of the code is (list & new line),

["SoftwareVersion='2.1'", "base= 'Win32GUI'"]

The output of the code is (list & space),

["SoftwareVersion='2.1'\nbase=", "'Win32GUI'"]

How does the function work?

This is a Python function called get_clipboard_text which takes two optional parameters, option and list_option. Here’s how it works:

  1. The function first imports the win32clipboard module which is a part of the pywin32 library for Windows.
  2. It then uses the OpenClipboard() function to open the clipboard and the GetClipboardData() function to get the current content of the clipboard.
  3. After retrieving the clipboard data, the function uses CloseClipboard() to close the clipboard.
  4. Depending on the value of the option parameter, the function either returns the clipboard data as text or as a list. If option is set to 'list', it splits the clipboard data by the value of the list_option parameter (which defaults to '\r\n', a common separator for lines of text in Windows). If option is set to 'text' (which is the default value for option), it simply returns the clipboard data as is.
  5. If option is set to any other value, the function displays a message box informing the user that the provided option is not correct.
  6. If the clipboard data ends with an empty line (which is common when copying multiple lines of text), the function removes it using the pop() method before returning the final clipboard data.

Overall, the get_clipboard_text function provides a convenient way to retrieve the current content of the clipboard in Python, and can be used in a variety of scenarios such as automating tasks that involve copying and pasting data between different applications.

Leave a Reply

Your email address will not be published. Required fields are marked *