python优化办公(excel)

from aip import AipOcr
from PIL import Image
from os import listdir
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 str

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')

名称栏目:python优化办公(excel)
当前地址:http://ybzwz.com/article/jccoos.html