您的位置:首頁/技術文章
文章詳情頁

python兩種獲取剪貼板內容的方法

瀏覽:8日期:2022-07-06 09:41:33

第一種

import win32clipboardimport time#速度快 容易出錯class niubi(): def lihai(self): while True: #jianting().main() t = jianting().main() print(t)class jianting(): def clipboard_get(self): '''獲取剪貼板數據''' win32clipboard.OpenClipboard() data = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT) win32clipboard.CloseClipboard() return data def main(self): '''后臺腳本:每隔0.2秒,讀取剪切板文本,檢查有無指定字符或字符串,如果有則執行替換''' # recent_txt 存放最近一次剪切板文本,初始化值只多執行一次paste函數讀取和替換 recent_txt = self.clipboard_get() while True: # txt 存放當前剪切板文本 txt = self.clipboard_get() # 剪切板內容和上一次對比如有變動,再進行內容判斷,判斷后如果發現有指定字符在其中的話,再執行替換 if txt != recent_txt: # print(f’txt:{txt}’) recent_txt = txt # 沒查到要替換的子串,返回None return recent_txt # 檢測間隔(延遲0.2秒) time.sleep(0.2)if __name__ == ’__main__’: niubi().lihai()

速度快,但很容易出錯, 一般人感覺不出來速度。 不建議使用。

方法二:

import pyperclipimport time#穩定不出錯class niubi(): def lihai(self): while True: #jianting().main() t = jianting().main() print(t)class jianting(): def clipboard_get(self): '''獲取剪貼板數據''' data = pyperclip.paste() #主要這里差別 return data def main(self): '''后臺腳本:每隔0.2秒,讀取剪切板文本,檢查有無指定字符或字符串,如果有則執行替換''' # recent_txt 存放最近一次剪切板文本,初始化值只多執行一次paste函數讀取和替換 recent_txt = self.clipboard_get() while True: # txt 存放當前剪切板文本 txt = self.clipboard_get() # 剪切板內容和上一次對比如有變動,再進行內容判斷,判斷后如果發現有指定字符在其中的話,再執行替換 if txt != recent_txt: # print(f’txt:{txt}’) recent_txt = txt # 沒查到要替換的子串,返回None return recent_txt # 檢測間隔(延遲0.2秒) time.sleep(0.2)if __name__ == ’__main__’: niubi().lihai()

我一般把第二種 用在程序中。

想要了解更多關于python的知識,資訊,實用工具歡迎關注python客棧

python兩種獲取剪貼板內容的方法

以上就是python兩種獲取剪貼板內容的方法的詳細內容,更多關于python 獲取剪貼板內容的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
国产综合久久一区二区三区