python截图识字并保存在xls表格中

准备:

  1.库:keyboard, pillow, baidu-aip, xlwt

  2.百度api: ai.baidu.com 注册并提供相关参数

 

代码:

 1 import keyboard
 2 from PIL import ImageGrab
 3 import time
 4 from aip import AipOcr
 5 import xlwt
 6 
 7 xls_path = r'C:\Users\Administrator\Desktop\test.xls'  #保存表格位置
 8 table = xlwt.Workbook()
 9 worksheet = table.add_sheet('sheet1')
10 
11 # 调用百度AI识别文字
12 API_ID = "你的AppID"
13 API_KEY = "你的API Key"
14 SECRET_KEY = "你的Secret Key"
15 client = AipOcr(API_ID, API_KEY, SECRET_KEY)
16 
17 
18 def get_file_content(filepath):
19     with open(filepath, 'rb') as fp:
20         return fp.read()
21 
22 
23 # 01 开始截图
24 print("开始截图")
25 num = 0
26 while 1:
27     keyboard.wait(hotkey='ctrl+alt+a')
28     print('键盘按下了"ctrl+alt+a"')
29     keyboard.wait('enter')
30     print('键盘按下了"enter"')
31     time.sleep(0.5)
32 
33     # 02 保存图片
34     image = ImageGrab.grabclipboard()
35     image.save('screen%s.png' % num)
36 
37     # 调用百度API文字识别
38     res_image = get_file_content('screen%s.png'%num)
39     text = client.basicAccurate(res_image)
40     textList = text['words_result']
41     print(textList)
42 
43     num1 = 0
44     for i in textList:
45         print(i['words'])
46         worksheet.write(num, num1, i['words'])
47         table.save(xls_path)
48         num += 1
49 
50     num += 1
51 
52     print('截图结束')

 

心得

  2020/1/13 在QQ登录状态下比较好用,缺点单独执行无截图区域显示

posted @ 2020-01-13 11:23  wangspy  阅读(201)  评论(0)    收藏  举报