python两种获取剪贴板内容的方法
作者:凹凸曼大人 时间:2021-03-05 11:18:56
第一种
import win32clipboard
import 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 pyperclip
import 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客栈
来源:https://www.cnblogs.com/aotumandaren/p/13906650.html
标签:python,剪贴板
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现压缩文件夹与解压缩zip文件的方法
2023-07-31 20:43:09
python复制文件代码实现
2022-08-29 09:28:11
PyTorch device与cuda.device用法介绍
2022-07-26 23:49:10
python实现换位加密算法的示例
2022-08-04 20:46:36
vuejs实现下拉框菜单选择
2023-09-23 08:49:54
![](https://img.aspxhome.com/file/2023/9/56299_0s.jpg)
sql server update 表的问题
2009-10-04 20:32:00
python 函数传参之传值还是传引用的分析
2021-08-20 13:06:34
![](https://img.aspxhome.com/file/2023/2/123802_0s.jpg)
python利用datetime模块计算程序运行时间问题
2022-05-24 03:13:41
asp中的on error resume next用法
2008-03-09 15:22:00
pandas读取CSV文件时查看修改各列的数据类型格式
2023-09-26 16:44:58
Python 将json序列化后的字符串转换成字典(推荐)
2021-11-17 12:36:51
asp 分页函数,可以显示 1,2,3,4,5... 前十页,后十页,下一页,上一页
2009-07-05 18:34:00
Tensorflow 2.4 搭建单层和多层 Bi-LSTM 模型
2021-03-30 16:25:06
使用Template格式化Python字符串的方法
2021-08-07 14:42:35
Python生成随机数详解流程
2023-03-31 15:21:33
![](https://img.aspxhome.com/file/2023/4/100954_0s.gif)
Python import自己的模块报错问题及解决
2023-11-09 15:57:08
![](https://img.aspxhome.com/file/2023/6/81466_0s.png)
CSS网页布局避免滥用DIV元素
2011-03-30 12:34:00
装载iframe 子页面,自适应高度
2008-11-20 12:53:00
浅谈Django学习migrate和makemigrations的差别
2021-05-11 03:36:11
python数据结构链表之单向链表(实例讲解)
2021-01-17 12:51:19
![](https://img.aspxhome.com/file/2023/6/71546_0s.png)