import os import tkinter as tk import matplotlib.pyplot as plt from tkinter import ttk from tkinter import filedialog from tkinter import messagebox from PIL import Image, ImageTk from aip import AipOcr from openpyxl import load_workbook ''' ### ###创建工作簿 wb = Workbook() #激活工作表 ws = wb.active #指定单元格写入数据 ws['A1'] = "学校名称" ws['B1'] = "课程名称" ws['C1'] = "姓名" ws['D1'] = "成绩" #设置列宽 ws.column_dimensions['A'].width = 25 ws.column_dimensions['B'].width = 25 #冻结首行 ws.freeze_panes = 'A2' #保存工作簿 wb.save('D:\\成绩录入\\学生成绩.xlsx') ''' # 图像识别 def reco_img_1(): # 创建进度条子窗口 top = tk.Toplevel(root) top.geometry('300x200') top.geometry('+500+260') # 设置标签 la = tk.Label(top, text="成绩录入中……", font=("微软雅黑 Light", 12)) la.place(x=100, y=50) # 设置进度条 progress = 0 # 进度条初始值为0 pv = tk.DoubleVar() # 保存变量 pb = ttk.Progressbar(top, length=200, variable=pv, maximum=100, orient=tk.HORIZONTAL) pb.pack(side='top', expand='yes') # 遍历‘D:\\成绩录入\\Pictures’路径下的文件夹 path = 'D:\\成绩录入\\Pictures' for home, dirs, files in os.walk(path): for filename in files: # 设置进度条步长 step = float(100.0 / (len(files) - 1)) top.update() # 更新子窗口 # 通过全名,识别出每一张图像(.jpg文件) fullname = os.path.join(home, filename) if fullname.endswith('.jpg'): image1 = open(fullname, 'rb') img1 = image1.read() # 百度智能云OCR:APP_ID API_KEY SECRET_KEY APP_ID = '25655448' API_KEY = 'wOd9goZPpsow1cC2aCFkSDi8' SECRET_KEY = 'Mhwd3XxZpz2gyLjKmuETaTVENXEXKK8W' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 设置“文字类型”参数为:手写印刷混排识别 options = {} options['words_type'] = 'handprint_mix' # 调用“试卷分析与识别”接口 results = client.docAnalysis(img1, options) # 将识别出的文字保存为字符串类型 text = '' for item1 in results['results']: for item2 in item1['words']['word']: text = text + item2 '''#进行字符串切片:得到“学校名称” for item3 in range(len(text)): if text[item3] in "课": text1 = text[:item3] break #进行字符串切片:得到“课程名称” for item4 in range(len(text)): if text[item4:item4+3] in "课程号": text2 = text[item3+11:item4]''' # 进行字符串切片:得到“姓名”和“成绩” if text[-3].isdigit(): # 如果姓名为两个字 text3 = text[-2:] text4 = text[-4:-2] elif text[-4].isdigit() and text[-3].isdigit() == False: # 如果姓名为三个字 text3 = text[-3:] text4 = text[-5:-3] else: # 如果姓名为四个字 text3 = text[-4:] text4 = text[-6:-4] # 加载工作簿 wb = load_workbook('D:\\成绩录入\\湖南农业大学学生成绩单.xlsx') ws = wb.active # 在工作表中写入成绩 # ws.append([text1,text2,text3,text4]) for i in ws.iter_cols(min_col=4, max_col=4, min_row=6): for j in i: if j.value in text3: ws.cell(j.row, 5).value = text4 # 保存工作簿 wb.save('D:\\成绩录入\\湖南农业大学学生成绩单.xlsx') # 进度条更新 progress += step pv.set(progress) # 弹出信息提示框 # mes = tk.messagebox.showinfo("成绩录入软件", "成绩录入成功!", type = 'okcancel') # if mes == 'ok': # reco_img() # else: tk.messagebox.showinfo("成绩录入软件", "成绩录入完成!") def reco_img_2(): # 弹出“请选择图像”对话框,位置为‘D:\\成绩录入\\Pictures’ file_path = filedialog.askopenfilename(title=u"成绩录入软件", initialdir='D:\\成绩录入\\Pictures') image2 = open(file_path, 'rb') img2 = image2.read() # 百度智能云OCR:APP_ID API_KEY SECRET_KEY APP_ID = '25655448' API_KEY = 'wOd9goZPpsow1cC2aCFkSDi8' SECRET_KEY = 'Mhwd3XxZpz2gyLjKmuETaTVENXEXKK8W' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 设置“文字类型”参数为:手写印刷混排识别 options = {} options['words_type'] = 'handprint_mix' # 调用“试卷分析与识别”接口 results = client.docAnalysis(img2, options) # 将识别出的文字保存为字符串类型 text = '' for item1 in results['results']: for item2 in item1['words']['word']: text = text + item2 # 进行字符串切片:得到“姓名”和“成绩” if text[-3].isdigit(): # 如果姓名为两个字 text3 = text[-2:] text4 = text[-4:-2] elif text[-4].isdigit() and text[-3].isdigit() == False: # 如果姓名为三个字 text3 = text[-3:] text4 = text[-5:-3] else: # 如果姓名为四个字 text3 = text[-4:] text4 = text[-6:-4] # 加载工作簿 wb = load_workbook('D:\\成绩录入\\湖南农业大学学生成绩单.xlsx') ws = wb.active # 在工作表中写入成绩 for i in ws.iter_cols(min_col=4, max_col=4, min_row=6): for j in i: if j.value in text3: ws.cell(j.row, 5).value = text4 # 保存工作簿 wb.save('D:\\成绩录入\\湖南农业大学学生成绩单.xlsx') # 弹出信息提示框 mes = messagebox.showinfo("成绩录入软件", "成绩录入成功!", type='okcancel') if mes == 'ok': reco_img_2() else: messagebox.showinfo("成绩录入软件", "成绩录入完成!") # 创建主窗口 root = tk.Tk() root.geometry('800x450') # 设置大小 root.title("成绩录入软件") # 设置标题 root.geometry('+250+100') # 设置位置 root.resizable(False, False) # 大小不可变 # 设置画布 canvas = tk.Canvas(root, width=800, height=450) image3 = Image.open("D:\\成绩录入\\草.png").resize((800, 450)) photo1 = ImageTk.PhotoImage(image3) canvas.create_image(400, 225, image=photo1) canvas.pack() # 设置“一键录入成绩”按钮 image4 = Image.open("D:\\成绩录入\\猫.png") photo2 = ImageTk.PhotoImage(image4) button1 = tk.Button(root, image=photo2, width=215, height=55, cursor='hand2', relief='flat', bd=5, text="一键录入成绩", font=("等线", 23, 'bold overstrike'), fg='black', compound='center', command=reco_img_1, takefocus=True) button1.place(x=550, y=338) # 设置按钮位置 # 设置“单独录入成绩”按钮 button2 = tk.Button(root, image=photo2, width=215, height=55, cursor='hand2', relief='flat', bd=5, text="单独录入成绩", font=("等线", 23, 'bold overstrike'), fg='black', compound='center', command=reco_img_2, takefocus=True) button2.place(x=330, y=338) # 设置按钮位置 root.mainloop()
cjlr代码记录
最新推荐文章于 2024-07-31 14:30:10 发布