好久没来了,更新了下代码
[Python] 纯文本查看 复制代码
from aip import AipOcr
import glob,time
import openpyxl
#此处是账号
client=AipOcr(APPID,APIKEY,SECRETKEY)
def get_path(path):#获取图片路径
return glob.iglob(r".\%s\**\*[jpg,png]"%(path),recursive=True)
def open_file(filePath) :#读取图片
with open(filePath,'rb') as fp:
return fp.read()
def get_text(fileName) :#转换文本
ls_dict={}
a=0
for i in fileName:
a=a+1
image = open_file(i)
id_card_side='front'
res = client.idcard(image,id_card_side)
try:
if '民族' in res['words_result'].keys():
xm=res['words_result']['姓名']['words']
sf=res['words_result']['公民身份号码']['words']
if xm not in ls_dict.keys():
print(xm,sf)
ls_dict[xm]=sf
except:
continue
print()
print(f' 共识别图片 {int(a)} 张!')
print(f' 其中身份证 {len(ls_dict)} 张!')
return ls_dict
def write_excel(bbb):#写入excel
wb = openpyxl.Workbook()
sheet = wb.active
sheet['A1'] = '姓名'
sheet['B1'] = '身份证号码'
row = 2
for i,j in bbb.items():
sheet.cell(row, 1, i)
sheet.cell(row, 2, j)
row += 1
wb.save("./人员明细.xlsx")
def main():
print()
print(' 正在识别……')
print()
path='aaa'
aaa=get_path(path)
bbb=get_text(aaa)
write_excel(bbb)
print()
print("已保存…")
input('按确定键退出!!!')
if __name__=='__main__':
main()
|