Home  >  Article  >  Backend Development  >  How can I retrieve text from the Windows clipboard using Python?

How can I retrieve text from the Windows clipboard using Python?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-30 22:41:30764browse

How can I retrieve text from the Windows clipboard using Python?

Reading Text from the Windows Clipboard in Python

To retrieve text from the Windows clipboard in Python, leverage the win32clipboard module within PyWin32.

Example:

<code class="python">import win32clipboard

# Set the clipboard data
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText('testing 123')
win32clipboard.CloseClipboard()

# Retrieve the clipboard data
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()

print(data)</code>

Important Reminder:

Ensure you close the clipboard after interacting with it using the CloseClipboard function. This allows other applications to access the clipboard. Avoid adding new data after closing it.

The above is the detailed content of How can I retrieve text from the Windows clipboard using Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn