PYTHON如何调取OCR识别模块识别并输出到EXCLE?

发布网友 发布时间:2022-04-22 23:18

我来回答

2个回答

热心网友 时间:2023-09-16 09:13

import qqai
from os import path
from win32com.client import Dispatch
import os
from datetime import datetime

def file_path():
global path_this_file
path_this_file = path.abspath('.') + "\\"
global path_excel
path_excel = path_this_file + '信息导出.xlsx'
global path_pic_file
path_pic_file = path_this_file + '照片'

def get_pic_name():
pic_list = []
for pic in os.listdir(path_pic_file):
pic_path = path_pic_file + '\\' + pic
pic_list.append(pic_path)
return pic_list

def HandwritingOCRImage(filename):
robot = qqai.vision.ocr.HandwritingOCR(app_id, app_key)
useless_list = ['登记表']
value_list = []
with open(filename, 'rb') as image_file:
result = robot.run(image_file)
item_list = result['data']['item_list']
for value in item_list:
words= value['itemstring']
if words in useless_list:
continue
else:
value_list.append(words)
return value_list

def get_useful_list(value_list):
key_list = ['姓名', '性别', '出生日期', '国家/地区', '民族', '职业', '手机号码', '固定电话', '证件类型', '证件有效期限', '证件号码', '通讯地址', '邮编']
useful_list = []
for words in value_list:
if words in key_list:
key_index = value_list.index(words)
next_index = key_index + 1
if value_list[next_index] in key_list:
useful_list.append('')
else:
if words == '证件号码':
ID_NUM = "'" + str(value_list[next_index]) #这边是为了避免科学计数法的问题
useful_list.append(ID_NUM)
else:
useful_list.append(value_list[next_index])
else:
continue
return useful_list

def put_into_excel(useful_list):
xl = Dispatch("Excel.Application")
xl.Visible = False # True是显示, False是隐藏
xl.DisplayAlerts = 0
excel_input = xl.Workbooks.Open(path_excel)
sheet = excel_input.Sheets('Sheet1')
max_row = sheet.UsedRange.Rows.Count
values = len(useful_list)
for i in range(values):
sheet.Cells(max_row + 1, i + 1).Value = str(useful_list[i])
excel_input.Save()
excel_input.Close()
xl.quit()

starttime = datetime.now()
"""腾讯AI开放平台 图片识别"""
app_id = '2110179251'
app_key = '******'
"""app_id , app_key 可以自己去腾讯AI开放平台注册,是免费的"""

file_path()
pic_list = get_pic_name()
for filename in pic_list:
value_list =HandwritingOCRImage(filename)
useful_list = get_useful_list(value_list)
put_into_excel(useful_list)
endtime = datetime.now()
total_time = (endtime - starttime).seconds
print(">>>成功录入信息{}条,总共耗时{}秒!".format(len(pic_list),total_time))

热心网友 时间:2023-09-16 09:13

现场需要根据打印的表格手工填写好内容,然后再在电脑上一个个录入进去,费时费力,所以想是否可以通过程序把照片内需要的数据读取出来并导出到excel表格里。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com