网络编程
位置:首页>> 网络编程>> Python编程>> python两种获取剪贴板内容的方法

python两种获取剪贴板内容的方法

作者:凹凸曼大人  发布时间:2021-03-05 11:18:56 

标签:python,剪贴板

第一种


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客栈

python两种获取剪贴板内容的方法

来源:https://www.cnblogs.com/aotumandaren/p/13906650.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com