点击上方 毛利学python,选择置顶或星标
第一时间送达Python 技术干货!
最近不想打字,直接截图然后识别出来文字,不就可以不用打吗?我就是太懒了。
keyboard
这个库让你可以控制和监控输入设备。
对于每一种输入设备,它包含一个子包来控制和监控该种输入设备:
安装
pip install keyboard==0.9.3
截图功能完成
当我按下alt+a就开始截图,注意要登着微信,然后就enter保存
import keyboard
from PIL import ImageGrab
import timedef screen():print('开始截图')# 使用微信的截图热键keyboard.wait('alt+a')# 保存keyboard.wait('enter')# 图片保存需要时间time.sleep(0.5)# 读取剪切板的图片image = ImageGrab.grabclipboard()# 保存图片image.save('screen.jpg')print('图片保存完成')if __name__ == '__main__':screen()
效果如下

截图识别
使用百度云来进行识别
百度的技术,阿里的运营,腾讯的产品,江湖常话。
创建文字识别的应用

官方的教程
https://cloud.baidu.com/doc/OCR/index.html
记得pip install aip
from aip import AipOcr""" 你的 APPID AK SK """
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
"""读取图片"""
def get_file_content(filepath):with open(filepath, 'rb') as f:return f.read()
def get_img_content(img):image_content = ''content = client.basicAccurate(image=img)# print(content)for words in content['words_result']:# print(words) # 字典image_content += words['words']print(image_content)with open('demo.txt','w',encoding='utf-8') as f:f.write(image_content)if __name__ == '__main__':# screen()img = get_file_content('screen.jpg')get_img_content(img)
效果如下

pyinstaller打包
一定要在代码最后面加上input(),这样打开exe不会一散而过
执行pyinstaller -F demo.py,搞定

还不如直接复制粘贴,但是如果复制不了就派上用场了
推荐阅读
添加微信[gopython3].回复:回复Go或者Python加对应技术群。