溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python操作剪切板的方法

發(fā)布時間:2020-07-07 14:56:49 來源:億速云 閱讀:314 作者:清晨 欄目:編程語言

這篇文章主要介紹python操作剪切板的方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

python中可以使用ctypes庫來操作剪切板:

1、調(diào)用get_clipboard() 獲取剪切板數(shù)據(jù)

if __name__ == '__main__':
    # 獲取剪切板內(nèi)字符串
    text_raw = get_clipboard()
    print('{0} {1}'.format(text_raw, type(text_raw)))
    
    try:
        text_str = text_raw.decode('utf_8')
        print('{0} {1}'.format(text_str, type(text_str)))
    except:
        print('剪切板為空。') 



剪切板為空時,輸出結果為:
None <class 'NoneType'>
剪切板為空。

復制一個字符串后運行上面的測試代碼(在這里我復制了 python ),輸出結果為:
b'Python' <class 'bytes'>
Python <class 'str'>

剪切板中無數(shù)據(jù)時,get_clipboard() 返回 None。

當剪切板中有數(shù)據(jù)時,get_clipboard() 將其以 bytes 格式返回;

使用 text_str = text_raw.decode('utf_8')將 bytes 轉化為 str。

2、調(diào)用 empty_clipboard() 清空剪切板

if __name__ == '__main__':
    # 清空剪切板
    empty_clipboard()
    text = get_clipboard()
    print(text)復制一個字符串后運行代碼,輸出結果為:None

3、調(diào)用 set_clipboard() 寫入剪切板

if __name__ == '__main__':
    # 向剪切板內(nèi)寫入 ascii 字符串
    set_clipboard('py!')
    text = get_clipboard()
    print(text)

輸出結果為:
b'py!'

以上是python操作剪切板的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI