python兩種獲取剪貼板內(nèi)容的方法
第一種
import win32clipboardimport time#速度快 容易出錯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)容和上一次對比如有變動,再進行內(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()
速度快,但很容易出錯, 一般人感覺不出來速度。 不建議使用。
方法二:
import pyperclipimport time#穩(wěn)定不出錯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)容和上一次對比如有變動,再進行內(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的知識,資訊,實用工具歡迎關(guān)注python客棧
以上就是python兩種獲取剪貼板內(nèi)容的方法的詳細內(nèi)容,更多關(guān)于python 獲取剪貼板內(nèi)容的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. java實現(xiàn)圖形化界面計算器2. javascript設(shè)計模式 ? 建造者模式原理與應(yīng)用實例分析3. Python使用oslo.vmware管理ESXI虛擬機的示例參考4. IntelliJ Idea2017如何修改緩存文件的路徑5. IDEA的Mybatis Generator駝峰配置問題6. 解決idea中yml文件不識別的問題7. IntelliJ IDEA設(shè)置條件斷點的方法步驟8. IIS Express 取代 ASP.NET Development Server的配置方法9. 一篇文章帶你了解JavaScript-對象10. Spring-Richclient 0.1.0 發(fā)布
