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

Mary-Kate Olsen
Release: 2024-10-30 22:41:30
Original
779 people have browsed it

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!