python基础--截图提取图中文字存入Excel表

python 实现截图自动存入Excel表

首先我先在桌面新建一个test.xls文件

import keyboard #监听按键库
from PIL import ImageGrab #图像处理库
import time
from aip import AipOcr #pip install baidu_aip\
import xlwt

biao = xlwt.Workbook()
worksheet = biao.add_sheet('sheet1')

# print("开始截图")

# 1. 截取图片
num=0

while 1:

    keyboard.wait(hotkey='ctrl+alt+a')
    print("键盘按下了'ctrl+alt+a'")
    keyboard.wait('enter')
    print("键盘按下了'enter'")

    # 模拟延迟,来解决grabclipboard函数的缓存问题(grabclipboard函数操作太快,它就会读取上一次的内容)
    time.sleep(0.1)

    # 2. 保存图片到电脑上
    image = ImageGrab.grabclipboard()
    image.save('screen%s.png'%(num))

    # 3. 调用百度AI识别图片内容
    APP_ID = '你的APP_ID'
    API_KEY = '你的API_KEY'
    SECRET_KEY = '你的SECRET_KEY'

    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

    # 获取图片的二进制数据
    def get_file_content(filePath):
        with open(filePath, 'rb') as fp:
            return fp.read()

    image = get_file_content('screen%s.png'%(num))

    """ 调用通用文字识别(高精度版) """
    text = client.basicAccurate(image)


    textList = text['words_result']


    print(textList)
    num1 = 0
    for i in textList:

        worksheet.write(num, num1, i['words'])
        biao.save('C:\\Users\\mxy\\Desktop\\test.xls')
        num1 += 1
    num+=1


    print("结束截图")


    原文作者:༺.雨巷.༻
    原文地址: https://blog.csdn.net/xue1163/article/details/107981040
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞