How to get Clipboard data?
- tinyytopic.com
- 0
- on Feb 27, 2023
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.
win32clipboard.OpenClipboard()
clipboard_text = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
# 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))
return
elif option == 'text':
clipboard_data = clipboard_text
else:
messagebox.showinfo('Message', 'Please provide correct option (text or list)!')
return
# 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('text'))
print(get_clipboard_text('list', '\n'))
print(get_clipboard_text('list', ' '))
Copy the text below and then execute the sample code above,
SoftwareVersion='2.1'
base= 'Win32GUI'
The output of the code is (text),
SoftwareVersion='2.1'
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:
- The function first imports the
win32clipboardmodule which is a part of thepywin32library for Windows. - It then uses the
OpenClipboard()function to open the clipboard and theGetClipboardData()function to get the current content of the clipboard. - After retrieving the clipboard data, the function uses
CloseClipboard()to close the clipboard. - Depending on the value of the
optionparameter, the function either returns the clipboard data as text or as a list. Ifoptionis set to'list', it splits the clipboard data by the value of thelist_optionparameter (which defaults to'\r\n', a common separator for lines of text in Windows). Ifoptionis set to'text'(which is the default value foroption), it simply returns the clipboard data as is. - If
optionis set to any other value, the function displays a message box informing the user that the provided option is not correct. - 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.