python兩種獲取剪貼板內(nèi)容的方法
第一種
import win32clipboardimport time#速度快 容易出錯(cuò)class niubi(): def lihai(self): while True: #jianting().main() t = jianting().main() print(t)class jianting(): def clipboard_get(self): '''獲取剪貼板數(shù)據(jù)''' win32clipboard.OpenClipboard() data = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT) win32clipboard.CloseClipboard() return data def main(self): '''后臺(tái)腳本:每隔0.2秒,讀取剪切板文本,檢查有無(wú)指定字符或字符串,如果有則執(zhí)行替換''' # recent_txt 存放最近一次剪切板文本,初始化值只多執(zhí)行一次paste函數(shù)讀取和替換 recent_txt = self.clipboard_get() while True: # txt 存放當(dāng)前剪切板文本 txt = self.clipboard_get() # 剪切板內(nèi)容和上一次對(duì)比如有變動(dòng),再進(jìn)行內(nèi)容判斷,判斷后如果發(fā)現(xiàn)有指定字符在其中的話,再執(zhí)行替換 if txt != recent_txt: # print(f’txt:{txt}’) recent_txt = txt # 沒(méi)查到要替換的子串,返回None return recent_txt # 檢測(cè)間隔(延遲0.2秒) time.sleep(0.2)if __name__ == ’__main__’: niubi().lihai()
速度快,但很容易出錯(cuò), 一般人感覺(jué)不出來(lái)速度。 不建議使用。
方法二:
import pyperclipimport time#穩(wěn)定不出錯(cuò)class niubi(): def lihai(self): while True: #jianting().main() t = jianting().main() print(t)class jianting(): def clipboard_get(self): '''獲取剪貼板數(shù)據(jù)''' data = pyperclip.paste() #主要這里差別 return data def main(self): '''后臺(tái)腳本:每隔0.2秒,讀取剪切板文本,檢查有無(wú)指定字符或字符串,如果有則執(zhí)行替換''' # recent_txt 存放最近一次剪切板文本,初始化值只多執(zhí)行一次paste函數(shù)讀取和替換 recent_txt = self.clipboard_get() while True: # txt 存放當(dāng)前剪切板文本 txt = self.clipboard_get() # 剪切板內(nèi)容和上一次對(duì)比如有變動(dòng),再進(jìn)行內(nèi)容判斷,判斷后如果發(fā)現(xiàn)有指定字符在其中的話,再執(zhí)行替換 if txt != recent_txt: # print(f’txt:{txt}’) recent_txt = txt # 沒(méi)查到要替換的子串,返回None return recent_txt # 檢測(cè)間隔(延遲0.2秒) time.sleep(0.2)if __name__ == ’__main__’: niubi().lihai()
我一般把第二種 用在程序中。
想要了解更多關(guān)于python的知識(shí),資訊,實(shí)用工具歡迎關(guān)注python客棧
以上就是python兩種獲取剪貼板內(nèi)容的方法的詳細(xì)內(nèi)容,更多關(guān)于python 獲取剪貼板內(nèi)容的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
