百度API批量识别表格

安装OCR Python SDK

支持Python版本:2.7.+ ,3.+

安装使用Python SDK有如下方式

  • 如果已安装pip,执行pip install baidu-aip即可。
  • 如果已安装setuptools,执行python setup.py install即可。

新建AipOcr

参考如下代码新建一个AipOcr:

from aip import AipOcr

""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

 

简单代码实现

 

import os
from aip import AipOcr
import requests
import time


""" 你的 APPID AK SK """
APP_ID = '1530xxxx'
API_KEY = 'fXp9dxxxxxxxxxxxxxxxxxxx'
SECRET_KEY = 'Ltvxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)


""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

"""文件下载"""
def file_download(url, name):
    res = requests.get(url)
    with open(name+'.xls', 'wb') as f:
        f.write(res.content)
 

filelist = os.listdir('./规划情况表') #设定图片目录


for file in filelist:
    image = get_file_content('./规划情况表/' + file)
    res = client.tableRecognitionAsync(image)
    requestId = res['result'][0]['request_id']
    while True:
        time.sleep(3)
        res2=client.getTableRecognitionResult(requestId)
        msg=res2['result']['ret_msg']
        if msg == '已完成':
            url = res2['result']['result_data']
            file_download(url, file[:-4])
            print(file + ' down')
            break
        else:
            time.sleep(1)

 

你可能感兴趣的:(教程)