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): '''后臺腳本:每隔0.2秒,讀取剪切板文本,檢查有無指定字符或字符串,如果有則執(zhí)行替換''' # recent_txt 存放最近一次剪切板文本,初始化值只多執(zhí)行一次paste函數(shù)讀取和替換 recent_txt = self.clipboard_get() while True: # txt 存放當(dāng)前剪切板文本 txt = self.clipboard_get() # 剪切板內(nèi)容和上一次對比如有變動(dòng),再進(jìn)行內(nèi)容判斷,判斷后如果發(fā)現(xiàn)有指定字符在其中的話,再執(zhí)行替換 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()
速度快,但很容易出錯(cuò), 一般人感覺不出來速度。 不建議使用。
方法二:
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): '''后臺腳本:每隔0.2秒,讀取剪切板文本,檢查有無指定字符或字符串,如果有則執(zhí)行替換''' # recent_txt 存放最近一次剪切板文本,初始化值只多執(zhí)行一次paste函數(shù)讀取和替換 recent_txt = self.clipboard_get() while True: # txt 存放當(dāng)前剪切板文本 txt = self.clipboard_get() # 剪切板內(nèi)容和上一次對比如有變動(dòng),再進(jìn)行內(nèi)容判斷,判斷后如果發(fā)現(xiàn)有指定字符在其中的話,再執(zhí)行替換 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()
我一般把第二種 用在程序中。
想要了解更多關(guān)于python的知識,資訊,實(shí)用工具歡迎關(guān)注python客棧
以上就是python兩種獲取剪貼板內(nèi)容的方法的詳細(xì)內(nèi)容,更多關(guān)于python 獲取剪貼板內(nèi)容的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 使用 kind 和 Docker 啟動(dòng)本地的 Kubernetes環(huán)境2. idea設(shè)置自動(dòng)導(dǎo)入依賴的方法步驟3. 解決python中import文件夾下面py文件報(bào)錯(cuò)問題4. XML入門精解之結(jié)構(gòu)與語法5. ASP基礎(chǔ)入門第八篇(ASP內(nèi)建對象Application和Session)6. IntelliJ IDEA設(shè)置編碼格式的方法7. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法8. idea自定義快捷鍵的方法步驟9. IntelliJ IDEA設(shè)置條件斷點(diǎn)的方法步驟10. Idea如何去除Mapper警告方法解析
