特别说明:本文使用的文字识别是AipOcr,对应的ID,KEY,SECRECT为你自己的,路径都是当前路径,操作excel用的是win32com
from aip import AipOc
from PIL import Image
from os import listdi
from os import rename
from os import remove
from os import system
from win32com.client import Dispatch
from os import getcwd
NAME_LIST = []
EXCEL_PATH = []
def get_name_by_pic(img):
img.save('test_tmp.jpg')
APP_ID = '你的ID'
API_KEY = '你的KEY'
SECRECT_KEY = '你的SECRECT_KEY '
client = AipOcr(APP_ID,API_KEY,SECRECT_KEY)
i = open('test_tmp.jpg','rb')
img = i.read()
message = client.basicGeneral(img)
name = message['words_result'][0]['words']
return name
def get_pic(pic,which_size):
img = Image.open(pic)
region = img.crop(which_size)
return region
def excel(WHICH_SHEET):
# WHICH_SHEET = 'Sheet1'
COL = 10
excel = Dispatch('Excel.Application')
excel.Visible = True
myExcel = excel.Workbooks.Open(getcwd()+'\\'+EXCEL_PATH[0])
mySheet = myExcel.Worksheets(WHICH_SHEET)
# mySheet.Cells(9,9).Interior.ColorIndex = 6 #表格背景 6为黄 5位蓝 4位绿 3为红 2为白 1为黑
flag = 0
len_name_list = int(len(NAME_LIST))
print(len(NAME_LIST))
for ROW in range(4,mySheet.usedrange.rows.count+1):
for i in NAME_LIST:
if i[2:5] == mySheet.Cells(ROW,COL).Value[2:5]:
NAME_LIST.remove(i)
flag += 1
print(flag,mySheet.Cells(ROW,COL).Value)
mySheet.Cells(ROW,COL).Interior.ColorIndex = 6
rename(i,mySheet.Cells(ROW,COL).Value+'.jpg')
break
print('\n-----未匹配成功:-------\n',len_name_list-int(flag),NAME_LIST)
myExcel.save
myExcel.close
excel.Visible = 0
def rm_space(str):
str = str.strip()
str = str.replace(' ','')
str = str.replace(' ','')
return st
def main():
old_file_name = listdir(r'./')
for jpg_excel in range(0,int(len(old_file_name))):
print(old_file_name[jpg_excel].split('.')[1])
if old_file_name[jpg_excel].split('.')[1] == 'jpg' or old_file_name[jpg_excel].split('.')[1] == 'png':
NAME_LIST.append(old_file_name[jpg_excel])
# elif old_file_name[jpg_excel].split('.')[1] == 'xlsx' or old_file_name[jpg_excel].split('.')[1] == 'xls' or old_file_name[jpg_excel].split('.')[1] == 'xl*':
elif old_file_name[jpg_excel].split('.')[1] == 'xlsx':
EXCEL_PATH.append(old_file_name[jpg_excel])
elif old_file_name[jpg_excel].split('.')[1] == 'xls':
EXCEL_PATH.append(old_file_name[jpg_excel])
# else:
# continue
for i in NAME_LIST:
print('获取切割后的图片',i)
new_img = get_pic(i,(200,675,600,710))
print('获取成功!\n识图中.............')
new_name = get_name_by_pic(new_img)
print('成功识别',new_name,'命名中......')
new_name = rm_space(new_name)
rename(i,new_name+'.jpg')
print('命名成功!即将进行第',i,'张')
remove('test_tmp.jpg')
main()
print('图片改名完成,即将对比excel')
excel('Sheet1')
print('excel对比完成!')
system('pause')
光有.PY文件怎么行,别人没有python啊,我们需要打包成.exe文件(注意:别被杀毒软件拦下来,以下操作在cmd框框执行,还得注意路径问题)
安装pyinstaller : pip install pyinstaller
使用pyinstaller: pyinstaller -F 你的.py文件 -i 你的ico图标(logo)
生成的.exe文件在 dist目录,built目录已经没用了
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。