python调用百度OCR识别证件+操作excel表格

python调用百度OCR识别证件+操作excel表格

如果要操作文件的话最好让程序知道文件存不存在,因为做了GUI界面,报错不会直接弹出来,异常捕获就用得比较频繁

因为面向对象没学好,GUI是用工具制作的,代码也直接写在类里了,不然实在不知道怎么调用另外一个类,我忽然想起能不能在GUI类里实例化功能类从而调用功能类?

-- coding: utf-8 --

import time, os
from win32com.client import Dispatch
import configparser
#from PIL import ImageGrab #pip install Pillow 报错Non-zero exit code (1)的话,升级pip到最新版本 python -m pip install --upgrade pip
“”" 使用百度api方式需要的模块 “”"
from aip import AipOcr #pip install baidu-aip
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.Qt import *
import PyQt5.sip
import images #导入窗口图标,这个库是自定义的把标题图像转为二进制

class MainForm(QWidget):
def init(self, name=‘MainForm’):
super(MainForm, self).init()
self.setWindowTitle(name)
#http: // ai.baidu.com / tech / ocr
# 修改成自己的信息,每个信息有限制次数,一起用的话很快用完就不能用了
# path = os.path.split(os.path.realpath(file))[0]
self.APP_ID = ‘xxxxxx’
self.API_KEY = ‘xxxxxxxxx’
self.SECRET_KEY = ‘xxxxxxxxx’
# self.filePath = path + r’\pic.png’
self.client = AipOcr(self.APP_ID, self.API_KEY, self.SECRET_KEY)
self.cwd = os.getcwd() # 获取当前程序文件位置

    '''得到桌面路径'''
    self.GetDesktopPath = os.path.join(os.path.expanduser("~"), 'Desktop')
def setupUi(self, Form):
    Form.setObjectName("Form")
    Form.resize(939, 741)
    Form.setMinimumSize(QtCore.QSize(939, 741))
    Form.setMaximumSize(QtCore.QSize(939, 741))

    self.verticalLayoutWidget = QtWidgets.QWidget(Form)
    self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 130, 391, 341))
    self.verticalLayoutWidget.setMinimumSize(QtCore.QSize(391, 341))
    self.verticalLayoutWidget.setMaximumSize(QtCore.QSize(391, 341))
    self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
    self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
    self.verticalLayout.setContentsMargins(0, 0, 0, 0)
    self.verticalLayout.setObjectName("verticalLayout")
    self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
    self.horizontalLayout_5.setObjectName("horizontalLayout_5")
    self.label_13 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_13.setMaximumSize(QtCore.QSize(64, 39))
    self.label_13.setStyleSheet("font: 12pt \"Arial\";")
    self.label_13.setObjectName("label_13")
    self.horizontalLayout_5.addWidget(self.label_13)
    self.biaozhiinput = QtWidgets.QPlainTextEdit(self.verticalLayoutWidget)
    self.biaozhiinput.setStyleSheet("font: 12pt \"Arial\";")
    self.biaozhiinput.setObjectName("biaozhiinput")
    self.horizontalLayout_5.addWidget(self.biaozhiinput)
    self.label_14 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_14.setMaximumSize(QtCore.QSize(64, 39))
    self.label_14.setStyleSheet("font: 12pt \"Arial\";")
    self.label_14.setObjectName("label_14")
    self.horizontalLayout_5.addWidget(self.label_14)
    self.baodaninput = QtWidgets.QPlainTextEdit(self.verticalLayoutWidget)
    self.baodaninput.setStyleSheet("font: 12pt \"Arial\";")
    self.baodaninput.setObjectName("baodaninput")
    self.horizontalLayout_5.addWidget(self.baodaninput)
    self.verticalLayout.addLayout(self.horizontalLayout_5)
    self.horizontalLayout = QtWidgets.QHBoxLayout()
    self.horizontalLayout.setObjectName("horizontalLayout")
    self.label = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label.setMaximumSize(QtCore.QSize(64, 39))
    self.label.setStyleSheet("font: 12pt \"Arial\";")
    self.label.setObjectName("label")
    self.horizontalLayout.addWidget(self.label)
    self.plainTextEdit = QtWidgets.QPlainTextEdit(self.verticalLayoutWidget)
    self.plainTextEdit.setStyleSheet("font: 12pt \"Arial\";")
    self.plainTextEdit.setObjectName("plainTextEdit")
    self.horizontalLayout.addWidget(self.plainTextEdit)
    self.label_11 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_11.setMaximumSize(QtCore.QSize(96, 39))
    self.label_11.setStyleSheet("font: 12pt \"Arial\";\n"
                                "background-color: rgb(170, 255, 127);")
    self.label_11.setObjectName("label_11")
    self.horizontalLayout.addWidget(self.label_11)
    self.plainTextEdit_10 = QtWidgets.QPlainTextEdit(self.verticalLayoutWidget)
    self.plainTextEdit_10.setStyleSheet("font: 12pt \"Arial\";")
    self.plainTextEdit_10.setPlainText("")
    self.plainTextEdit_10.setObjectName("plainTextEdit_10")
    self.horizontalLayout.addWidget(self.plainTextEdit_10)
    self.verticalLayout.addLayout(self.horizontalLayout)
    self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
    self.horizontalLayout_2.setObjectName("horizontalLayout_2")
    self.label_2 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_2.setMinimumSize(QtCore.QSize(64, 41))
    self.label_2.setMaximumSize(QtCore.QSize(64, 41))
    self.label_2.setStyleSheet("font: 12pt \"Arial\";")
    self.label_2.setObjectName("label_2")
    self.horizontalLayout_2.addWidget(self.label_2)
    self.plainTextEdit_2 = QtWidgets.QPlainTextEdit(self.verticalLayoutWidget)
    self.plainTextEdit_2.setStyleSheet("font: 12pt \"Arial\";")
    self.plainTextEdit_2.setObjectName("plainTextEdit_2")
    self.horizontalLayout_2.addWidget(self.plainTextEdit_2)
    self.verticalLayout.addLayout(self.horizontalLayout_2)
    self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
    self.horizontalLayout_4.setObjectName("horizontalLayout_4")
    self.label_4 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_4.setMinimumSize(QtCore.QSize(48, 40))
    self.label_4.setMaximumSize(QtCore.QSize(48, 40))
    self.label_4.setStyleSheet("font: 12pt \"Arial\";")
    self.label_4.setObjectName("label_4")
    self.horizontalLayout_4.addWidget(self.label_4)
    self.plainTextEdit_4 = QtWidgets.QPlainTextEdit(self.verticalLayoutWidget)
    self.plainTextEdit_4.setMaximumSize(QtCore.QSize(333, 40))
    self.plainTextEdit_4.setStyleSheet("font: 12pt \"Arial\";")
    self.plainTextEdit_4.setObjectName("plainTextEdit_4")
    self.horizontalLayout_4.addWidget(self.plainTextEdit_4)
    self.verticalLayout.addLayout(self.horizontalLayout_4)
    self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
    self.horizontalLayout_6.setObjectName("horizontalLayout_6")
    self.label_6 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_6.setMinimumSize(QtCore.QSize(80, 40))
    self.label_6.setMaximumSize(QtCore.QSize(80, 40))
    self.label_6.setStyleSheet("font: 12pt \"Arial\";")
    self.label_6.setObjectName("label_6")
    self.horizontalLayout_6.addWidget(self.label_6)
    self.plainTextEdit_6 = QtWidgets.QPlainTextEdit(self.verticalLayoutWidget)
    self.plainTextEdit_6.setMaximumSize(QtCore.QSize(301, 40))
    self.plainTextEdit_6.setStyleSheet("font: 12pt \"Arial\";")
    self.plainTextEdit_6.setObjectName("plainTextEdit_6")
    self.horizontalLayout_6.addWidget(self.plainTextEdit_6)
    self.verticalLayout.addLayout(self.horizontalLayout_6)
    self.horizontalLayout_7 = QtWidgets.QHBoxLayout()
    self.horizontalLayout_7.setObjectName("horizontalLayout_7")
    self.label_7 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_7.setMaximumSize(QtCore.QSize(71, 40))
    self.label_7.setStyleSheet("font: 12pt \"Arial\";")
    self.label_7.setObjectName("label_7")
    self.horizontalLayout_7.addWidget(self.label_7)
    self.plainTextEdit_7 = QtWidgets.QPlainTextEdit(self.verticalLayoutWidget)
    self.plainTextEdit_7.setStyleSheet("font: 12pt \"Arial\";")
    self.plainTextEdit_7.setObjectName("plainTextEdit_7")
    self.horizontalLayout_7.addWidget(self.plainTextEdit_7)
    self.label_3 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_3.setMinimumSize(QtCore.QSize(71, 35))
    self.label_3.setStyleSheet("font: 12pt \"Arial\";")
    self.label_3.setObjectName("label_3")
    self.horizontalLayout_7.addWidget(self.label_3)
    self.plainTextEdit_3 = QtWidgets.QPlainTextEdit(self.verticalLayoutWidget)
    self.plainTextEdit_3.setStyleSheet("font: 10pt \"Arial\";")
    self.plainTextEdit_3.setObjectName("plainTextEdit_3")
    self.horizontalLayout_7.addWidget(self.plainTextEdit_3)
    self.verticalLayout.addLayout(self.horizontalLayout_7)
    self.horizontalLayout_8 = QtWidgets.QHBoxLayout()
    self.horizontalLayout_8.setObjectName("horizontalLayout_8")
    self.label_8 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_8.setMaximumSize(QtCore.QSize(91, 40))
    self.label_8.setStyleSheet("font: 12pt \"Arial\";")
    self.label_8.setObjectName("label_8")
    self.horizontalLayout_8.addWidget(self.label_8)
    self.plainTextEdit_8 = QtWidgets.QPlainTextEdit(self.verticalLayoutWidget)
    self.plainTextEdit_8.setMaximumSize(QtCore.QSize(111, 35))
    self.plainTextEdit_8.setStyleSheet("font: 12pt \"Arial\";")
    self.plainTextEdit_8.setObjectName("plainTextEdit_8")
    self.horizontalLayout_8.addWidget(self.plainTextEdit_8)
    self.label_5 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_5.setMaximumSize(QtCore.QSize(80, 39))
    self.label_5.setStyleSheet("font: 12pt \"Arial\";")
    self.label_5.setObjectName("label_5")
    self.horizontalLayout_8.addWidget(self.label_5)
    self.plainTextEdit_5 = QtWidgets.QPlainTextEdit(self.verticalLayoutWidget)
    self.plainTextEdit_5.setMaximumSize(QtCore.QSize(111, 35))
    self.plainTextEdit_5.setStyleSheet("font: 11pt \"Arial\";")
    self.plainTextEdit_5.setObjectName("plainTextEdit_5")
    self.horizontalLayout_8.addWidget(self.plainTextEdit_5)
    self.verticalLayout.addLayout(self.horizontalLayout_8)
    self.horizontalLayout_9 = QtWidgets.QHBoxLayout()
    self.horizontalLayout_9.setObjectName("horizontalLayout_9")
    self.label_9 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_9.setMinimumSize(QtCore.QSize(80, 40))
    self.label_9.setMaximumSize(QtCore.QSize(80, 40))
    self.label_9.setStyleSheet("font: 12pt \"Arial\";")
    self.label_9.setObjectName("label_9")
    self.horizontalLayout_9.addWidget(self.label_9)
    self.plainTextEdit_9 = QtWidgets.QPlainTextEdit(self.verticalLayoutWidget)
    self.plainTextEdit_9.setMaximumSize(QtCore.QSize(301, 40))
    self.plainTextEdit_9.setStyleSheet("font: 12pt \"Arial\";")
    self.plainTextEdit_9.setObjectName("plainTextEdit_9")
    self.horizontalLayout_9.addWidget(self.plainTextEdit_9)
    self.verticalLayout.addLayout(self.horizontalLayout_9)
    self.horizontalLayoutWidget_10 = QtWidgets.QWidget(Form)
    self.horizontalLayoutWidget_10.setGeometry(QtCore.QRect(10, 480, 391, 33))
    self.horizontalLayoutWidget_10.setObjectName("horizontalLayoutWidget_10")
    self.horizontalLayout_10 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_10)
    self.horizontalLayout_10.setContentsMargins(0, 0, 0, 0)
    self.horizontalLayout_10.setObjectName("horizontalLayout_10")
    self.label_10 = QtWidgets.QLabel(self.horizontalLayoutWidget_10)
    self.label_10.setMaximumSize(QtCore.QSize(151, 29))
    self.label_10.setStyleSheet("font: 12pt \"Arial\";\n"
                                "background-color: rgb(170, 255, 0);")
    self.label_10.setObjectName("label_10")
    self.horizontalLayout_10.addWidget(self.label_10)
    self.qibaoriqiinput = QtWidgets.QPlainTextEdit(self.horizontalLayoutWidget_10)
    self.qibaoriqiinput.setMaximumSize(QtCore.QSize(232, 31))
    self.qibaoriqiinput.setStyleSheet("font: 12pt \"Arial\";font: 12pt \"Arial\";")
    self.qibaoriqiinput.setObjectName("qibaoriqiinput")
    self.horizontalLayout_10.addWidget(self.qibaoriqiinput)
    self.pushButton_2 = QtWidgets.QPushButton(Form)
    self.pushButton_2.setGeometry(QtCore.QRect(430, 0, 101, 51))
    self.pushButton_2.setMaximumSize(QtCore.QSize(101, 51))
    self.pushButton_2.setStyleSheet("font: 12pt \"Arial\";")
    self.pushButton_2.setObjectName("pushButton_2")
    self.pushButton_3 = QtWidgets.QPushButton(Form)
    self.pushButton_3.setGeometry(QtCore.QRect(430, 50, 101, 51))
    self.pushButton_3.setMaximumSize(QtCore.QSize(101, 51))
    self.pushButton_3.setStyleSheet("font: 12pt \"Arial\";")
    self.pushButton_3.setObjectName("pushButton_3")
    self.pushButton_4 = QtWidgets.QPushButton(Form)
    self.pushButton_4.setGeometry(QtCore.QRect(530, 50, 101, 51))
    self.pushButton_4.setMaximumSize(QtCore.QSize(101, 51))
    self.pushButton_4.setStyleSheet("font: 12pt \"Arial\";")
    self.pushButton_4.setObjectName("pushButton_4")
    self.pushButton_5 = QtWidgets.QPushButton(Form)
    self.pushButton_5.setGeometry(QtCore.QRect(530, 0, 101, 51))
    self.pushButton_5.setMaximumSize(QtCore.QSize(101, 51))
    self.pushButton_5.setStyleSheet("font: 12pt \"Arial\";")
    self.pushButton_5.setObjectName("pushButton_5")
    self.textEdit = QtWidgets.QTextEdit(Form)
    self.textEdit.setGeometry(QtCore.QRect(430, 180, 511, 111))
    self.textEdit.setStyleSheet("font: 11pt \"Arial\";\n"
                                "background-color: rgb(255, 253, 248);")
    self.textEdit.setObjectName("textEdit")
    self.labIMG = QtWidgets.QLabel(Form)
    self.labIMG.setGeometry(QtCore.QRect(430, 295, 501, 431))
    self.labIMG.setStyleSheet("font: 12pt \"Arial\";\n"
                              "background-color: rgb(170, 255, 255);")
    self.labIMG.setObjectName("labIMG")
    self.calendarWidget = QtWidgets.QCalendarWidget(Form)
    self.calendarWidget.setGeometry(QtCore.QRect(10, 520, 391, 211))
    self.calendarWidget.setObjectName("calendarWidget")
    self.horizontalLayoutWidget_3 = QtWidgets.QWidget(Form)
    self.horizontalLayoutWidget_3.setGeometry(QtCore.QRect(10, 0, 391, 41))
    self.horizontalLayoutWidget_3.setObjectName("horizontalLayoutWidget_3")
    self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_3)
    self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
    self.horizontalLayout_3.setObjectName("horizontalLayout_3")
    self.label_15 = QtWidgets.QLabel(self.horizontalLayoutWidget_3)
    self.label_15.setMaximumSize(QtCore.QSize(112, 39))
    self.label_15.setStyleSheet("font: 12pt \"Arial\";")
    self.label_15.setObjectName("label_15")
    self.horizontalLayout_3.addWidget(self.label_15)
    self.taodainput = QtWidgets.QPlainTextEdit(self.horizontalLayoutWidget_3)
    self.taodainput.setMaximumSize(QtCore.QSize(190, 39))
    self.taodainput.setObjectName("taodainput")
    self.horizontalLayout_3.addWidget(self.taodainput)
    self.taodaButton = QtWidgets.QPushButton(self.horizontalLayoutWidget_3)
    self.taodaButton.setMaximumSize(QtCore.QSize(75, 39))
    self.taodaButton.setStyleSheet("font: 12pt \"楷体\";")
    self.taodaButton.setObjectName("taodaButton")
    self.horizontalLayout_3.addWidget(self.taodaButton)
    self.horizontalLayoutWidget_4 = QtWidgets.QWidget(Form)
    self.horizontalLayoutWidget_4.setGeometry(QtCore.QRect(10, 40, 391, 43))
    self.horizontalLayoutWidget_4.setObjectName("horizontalLayoutWidget_4")
    self.horizontalLayout_11 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_4)
    self.horizontalLayout_11.setContentsMargins(0, 0, 0, 0)
    self.horizontalLayout_11.setObjectName("horizontalLayout_11")
    self.label_18 = QtWidgets.QLabel(self.horizontalLayoutWidget_4)
    self.label_18.setMaximumSize(QtCore.QSize(141, 39))
    self.label_18.setStyleSheet("font: 12pt \"Arial\";\n"
                                "background-color: rgb(170, 255, 255);")
    self.label_18.setObjectName("label_18")
    self.horizontalLayout_11.addWidget(self.label_18)
    self.OCRleixingBT = QtWidgets.QComboBox(self.horizontalLayoutWidget_4)
    self.OCRleixingBT.setMinimumSize(QtCore.QSize(81, 41))
    self.OCRleixingBT.setMaximumSize(QtCore.QSize(81, 41))
    self.OCRleixingBT.setStyleSheet("font: 12pt \"楷体\";")
    self.OCRleixingBT.setObjectName("OCRleixingBT")
    self.OCRleixingBT.addItem("")
    self.OCRleixingBT.addItem("")
    self.OCRleixingBT.addItem("")
    self.OCRleixingBT.addItem("")
    self.horizontalLayout_11.addWidget(self.OCRleixingBT)
    self.label_16 = QtWidgets.QLabel(self.horizontalLayoutWidget_4)
    self.label_16.setMaximumSize(QtCore.QSize(101, 39))
    self.label_16.setStyleSheet("font: 12pt \"Arial\";\n"
                                "background-color: rgb(170, 255, 255);")
    self.label_16.setObjectName("label_16")
    self.horizontalLayout_11.addWidget(self.label_16)
    self.OCRleixingBT_2 = QtWidgets.QComboBox(self.horizontalLayoutWidget_4)
    self.OCRleixingBT_2.setMinimumSize(QtCore.QSize(81, 41))
    self.OCRleixingBT_2.setMaximumSize(QtCore.QSize(81, 41))
    self.OCRleixingBT_2.setStyleSheet("font: 12pt \"楷体\";")
    self.OCRleixingBT_2.setObjectName("OCRleixingBT_2")
    self.OCRleixingBT_2.addItem("")
    self.OCRleixingBT_2.addItem("")
    self.OCRleixingBT_2.addItem("")
    self.OCRleixingBT_2.addItem("")
    self.horizontalLayout_11.addWidget(self.OCRleixingBT_2)
    self.horizontalLayoutWidget = QtWidgets.QWidget(Form)
    self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 90, 391, 41))
    self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
    self.horizontalLayout_12 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
    self.horizontalLayout_12.setContentsMargins(0, 0, 0, 0)
    self.horizontalLayout_12.setObjectName("horizontalLayout_12")
    self.label_17 = QtWidgets.QLabel(self.horizontalLayoutWidget)
    self.label_17.setMaximumSize(QtCore.QSize(141, 39))
    self.label_17.setStyleSheet("font: 12pt \"Arial\";\n"
                                "")
    self.label_17.setObjectName("label_17")
    self.horizontalLayout_12.addWidget(self.label_17)
    self.XZIMG = QtWidgets.QPushButton(self.horizontalLayoutWidget)
    self.XZIMG.setMaximumSize(QtCore.QSize(81, 49))
    self.XZIMG.setStyleSheet("font: 12pt \"楷体\";")
    self.XZIMG.setObjectName("XZIMG")
    self.horizontalLayout_12.addWidget(self.XZIMG)
    self.label_19 = QtWidgets.QLabel(self.horizontalLayoutWidget)
    self.label_19.setMaximumSize(QtCore.QSize(141, 39))
    self.label_19.setStyleSheet("font: 12pt \"Arial\";\n"
                                "")
    self.label_19.setObjectName("label_19")
    self.horizontalLayout_12.addWidget(self.label_19)
    self.XZIMG_2 = QtWidgets.QPushButton(self.horizontalLayoutWidget)
    self.XZIMG_2.setMaximumSize(QtCore.QSize(81, 49))
    self.XZIMG_2.setStyleSheet("font: 12pt \"楷体\";")
    self.XZIMG_2.setObjectName("XZIMG_2")
    self.horizontalLayout_12.addWidget(self.XZIMG_2)
    '''日历限制'''
    self.calendarWidget.setMinimumDate(QDate.currentDate().addDays(0))
    self.calendarWidget.setMaximumDate(QDate.currentDate().addDays(90))
    self.calendarWidget.setGridVisible(True)
    # vlayout = QVBoxLayout()
    # self.dateEdit = QDateTimeEdit(QDateTime.currentDateTime(), self)
    # # 设置显示的格式
    # self.dateEdit.setDisplayFormat("yyyy-MM-dd")
    # # 设置最小日期
    # self.dateEdit.setMinimumDate(QDate.currentDate().addDays(-1))
    # # 设置最大日期
    # self.dateEdit.setMaximumDate(QDate.currentDate().addDays(90))
    # self.dateEdit.setGeometry(QRect(107, 440, 131, 40))
    # self.dateEdit.setMaximumSize(QSize(131, 40))
    # self.dateEdit.setStyleSheet("font: 12pt \"Arial\";")
    # # 单击下拉箭头就会弹出日历控件,不在范围内的日期是无法选择的
    # self.dateEdit.setCalendarPopup(True)
    # # 信号槽函数
    # # 改变日期
    self.pushButton_5.clicked.connect(self.qingkong)
    self.pushButton_3.clicked.connect(self.printbiaozhi)
    self.pushButton_4.clicked.connect(self.printbaodan)
    self.pushButton_2.clicked.connect(self.baocunsuju)
    self.XZIMG.clicked.connect(self.xuanztp)
    self.XZIMG_2.clicked.connect(self.xuanztp11)
    self.taodaButton.clicked.connect(self.xuanztp2)#套打格式路径
    self.calendarWidget.clicked[QtCore.QDate].connect(self.showDate)

    self.retranslateUi(Form)
    QtCore.QMetaObject.connectSlotsByName(Form)
    self.ini_chushihua()

def ini_chushihua(self):
    '''
                    套打初始化路径
                    '''
    inilujing = os.access(self.GetDesktopPath + '\\lujing.ini', False)
    if inilujing == True:
        #  实例化configParser对象
        config = configparser.ConfigParser()
        # -read读取ini文件
        config.read(self.GetDesktopPath + '\\lujing.ini', encoding='GB18030')
        list = []
        list = config.sections()  # 获取到配置文件中所有分组名称
        if 'type' not in list:
            pass
        else:
            lujing = config.get('type', 'lujing')
            self.filePath2 = lujing
            self.taodainput.setPlainText(self.filePath2)

        '''初始化卡号单号'''
        try:
            xlApp = Dispatch('Excel.Application')
            xlApp.Visible = False
            xlApp.DisplayAlerts = 0  # 关闭系统警告
            xlBook = xlApp.Workbooks.Open(self.filePath2, False)
            # xlBook.Application.Run('打印')  #运行宏
            xlSheet = xlApp.Worksheets('信息录入')
            self.biaozi1 = int(xlSheet.Range('B3').Value)
            self.biaodan1 = int(xlSheet.Range('B4').Value)
            print(self.biaodan1)
            self.biaozhiinput.setPlainText(str(self.biaozi1))
            self.baodaninput.setPlainText(str(self.biaodan1))
            xlBook.Close(True)
            xlApp.Quit()



        except Exception as e:
            print(e)
            xlApp = Dispatch('Excel.Application')
            if xlApp:
                for wrk in xlApp.workbooks:
                    wrk.Close(False)
                xlApp.Quit()
def showDate(self, date):
    self.qibaoriqiinput.setPlainText('')
    self.qibaoriqiinput.setPlainText(date.toString("yyyy-MM-dd"))

def onDateChanged(self, date):
    riqi = date.toString("yyyy-MM-dd")
    print(riqi)

    # xlApp = win32com.client.DispatchEx("Excel.Application")
    # xlApp.Visible = True
    # xlApp.DisplayAlerts = 0
    # xlBook = xlApp.Workbooks.Open(self.filePath2, False)
    # #xlBook.Application.Run(VBA)  # 宏
    #
    # xlBook.Close(True)
    # xlApp.quit()
    tdgs = self.plainTextEdit_11.toPlainText()  # 套打格式
    print(tdgs)
    try:
        xlApp = Dispatch('Excel.Application')
        xlApp.Visible = False
        xlApp.DisplayAlerts = 0  # 关闭系统警告
        xlBook = xlApp.Workbooks.Open(self.filePath2, False)
        # xlBook.Application.Run('打印')  #运行宏
        xlSheet = xlApp.Worksheets('信息录入')
        xlSheet.Cells(13, 2).Value = riqi  # 输入起保日期
        xlBook.SaveAs(self.filePath2)
        xlBook.Close(True)
        xlApp.Quit()  # exit app
        # r_xls = open_workbook(self.filePath2)  # 读取excel文件
        # #row = r_xls.sheets()[0].nrows  # 获取已有的行数
        # excel = copy(r_xls)  # 将xlrd的对象转化为xlwt的对象
        # table = excel.get_sheet(3)  # 获取要操作的sheet
        #
        # # 对excel表追加一行内容
        # table.write(15, 2, u'内容1')  # 括号内分别为行数、列数、内容
        print('已经写进日期')
        # # table.write(row, 1, u'内容2')
        # # table.write(row, 2, u'内容3')
        #
        # excel.save(self.filePath2)  # 保存并覆盖文件
    except Exception as e:
        print(e)

def qingkong(self):
    self.print('界面已清空')
    self.plainTextEdit.setPlainText('')
    self.plainTextEdit_2.setPlainText('')
    self.plainTextEdit_4.setPlainText('')
    self.plainTextEdit_6.setPlainText('')
    self.plainTextEdit_7.setPlainText('')
    self.plainTextEdit_3.setPlainText('')
    self.plainTextEdit_8.setPlainText('')
    self.plainTextEdit_9.setPlainText('')
    self.plainTextEdit_5.setPlainText('')
    try:
        xlApp = Dispatch('Excel.Application')
        xlApp.Visible = False
        xlApp.DisplayAlerts = 0  # 关闭系统警告
        xlBook = xlApp.Workbooks.Open(self.filePath2, False)
        # xlBook.Application.Run('打印')  #运行宏
        xlSheet = xlApp.Worksheets('信息录入')
        self.biaozi1 = int(xlSheet.Range('B3').Value)
        self.biaodan1 = int(xlSheet.Range('B4').Value)
        print(self.biaodan1)
        self.biaozhiinput.setPlainText(str(self.biaozi1+1))
        self.baodaninput.setPlainText(str(self.biaodan1+1))
        xlSheet.Cells(3, 2).Value = self.biaozi1+1
        xlSheet.Cells(4, 2).Value = self.biaodan1+1
        xlBook.SaveAs(self.filePath2)
        xlBook.Close(True)
        xlApp.Quit()



    except Exception as e:
        print(e)
        xlApp = Dispatch('Excel.Application')
        if xlApp:
            for wrk in xlApp.workbooks:
                wrk.Close(False)
            xlApp.Quit()

def baocunsuju(self):
    self.print('保存数据')
    try:



        xlApp = Dispatch('Excel.Application')
        xlApp.Visible = False
        xlApp.DisplayAlerts = 0  # 关闭系统警告
        xlBook = xlApp.Workbooks.Open(self.filePath2, False)
        # xlBook.Application.Run('打印')  #运行宏
        xlSheet = xlApp.Worksheets('信息录入')
        shuju = []

        sj = self.plainTextEdit_6.toPlainText()  # 手机
        xm = self.plainTextEdit.toPlainText()  # 姓名
        sfhm = self.plainTextEdit_2.toPlainText()  # 身份证
        cpxh = self.plainTextEdit_3.toPlainText()  # 车辆型号
        zz = self.plainTextEdit_4.toPlainText()  # 住址
        cdrq = self.plainTextEdit_5.toPlainText()  # 初登日期
        cphm = self.plainTextEdit_7.toPlainText()  # 车牌
        fdjhm = self.plainTextEdit_8.toPlainText()  # 发动机
        cjhm = self.plainTextEdit_9.toPlainText()  # 车架号
        ccs = self.plainTextEdit_10.toPlainText()  # 车船税
        qbrq = self.qibaoriqiinput.toPlainText()  # 起保日期

        xuhao = int(xlSheet.Range('B1').Value)
        biaozi = int(xlSheet.Range('B3').Value)
        biaodan = int(xlSheet.Range('B4').Value)
        shuju.append(xuhao)
        shuju.append(biaozi)
        shuju.append(biaodan)
        shuju.append(xm)
        shuju.append(sfhm)
        shuju.append(zz)
        shuju.append(sj)
        shuju.append(cphm)
        shuju.append(cpxh)
        shuju.append(fdjhm)
        shuju.append(cjhm)
        shuju.append(cdrq)
        shuju.append(ccs)
        shuju.append(qbrq)
        xlSheet.Cells(1, 2).Value = shuju[0]
        xlSheet.Cells(3, 2).Value = shuju[1]
        xlSheet.Cells(4, 2).Value = shuju[2]
        xlSheet.Cells(5, 2).Value = shuju[3]  # 姓名
        xlSheet.Cells(6, 2).Value = shuju[4]
        xlSheet.Cells(7, 2).Value = shuju[5]
        xlSheet.Cells(8, 2).Value = shuju[6]
        xlSheet.Cells(9, 2).Value = shuju[7]
        xlSheet.Cells(10, 2).Value = shuju[8]
        xlSheet.Cells(11, 2).Value = shuju[9]
        xlSheet.Cells(12, 2).Value = shuju[10]
        xlSheet.Cells(14, 2).Value = shuju[11]
        xlSheet.Cells(16, 2).Value = shuju[12]
        xlSheet.Cells(13, 2).Value = shuju[13]
        xlSheet = xlApp.Worksheets('记录')
        x =xlSheet.Range("a65536").End(-4162).Row + 1 #取得“记录”表中最后一个空行的行号(即写入位置)
        print('获取记录行数{}'.format(x))
        self.print('获取记录行数{}'.format(x))
        i=0
        for tianxie in shuju:
            i+=1
            xlSheet.Cells(x, i).Value = tianxie
            print(tianxie)
            print('i={}'.format(i))
        xlBook.SaveAs(self.filePath2)
        xlBook.Close(True)
        xlApp.Quit()  # exit app
        self.print('数据已保存到”信息录入“和”记录“表单中')
        # for i in shuju:
        #     print(i)
        return shuju
    except Exception as e:
        xlApp = Dispatch('Excel.Application')
        if xlApp:
            for wrk in xlApp.workbooks:
                wrk.Close(False)
            xlApp.Quit()
        self.print(e)
        print(e)

def printbiaozhi(self):
    self.print('打印标志')
    try:

        xlApp = Dispatch('Excel.Application')
        xlApp.Visible = False
        xlApp.DisplayAlerts = 0  # 关闭系统警告
        xlBook = xlApp.Workbooks.Open(self.filePath2, False)
        # xlBook.Application.Run('打印')  #运行宏
        xlSheet = xlApp.Worksheets('保卡')#切换
        xlSheet.PrintOut()#向打印机发送打印文件


        xlBook.SaveAs(self.filePath2)
        xlBook.Close(True)
        xlApp.Quit()  # exit app
        self.print('标志已发送到默认打印机中,请放入标志进行打印')


    except Exception as e:
        xlApp = Dispatch('Excel.Application')
        if xlApp:
            for wrk in xlApp.workbooks:
                wrk.Close(False)
            xlApp.Quit()
        self.print(e)
        print(e)

def printbaodan(self):
    self.print('打印保单')
    try:

        xlApp = Dispatch('Excel.Application')
        xlApp.Visible = False
        xlApp.DisplayAlerts = 0  # 关闭系统警告
        xlBook = xlApp.Workbooks.Open(self.filePath2, False)
        # xlBook.Application.Run('打印')  #运行宏
        xlSheet = xlApp.Worksheets('保单打印')  # 切换
        xlSheet.PrintOut()  # 向打印机发送打印文件

        xlBook.SaveAs(self.filePath2)
        xlBook.Close(True)
        xlApp.Quit()  # exit app
        self.print('保单已发送到默认打印机中,请放入保单进行打印')


    except Exception as e:
        xlApp = Dispatch('Excel.Application')
        if xlApp:
            for wrk in xlApp.workbooks:
                wrk.Close(False)
            xlApp.Quit()
        self.print(e)
        print(e)

def retranslateUi(self, Form):
    _translate = QtCore.QCoreApplication.translate
    Form.setWindowTitle(_translate("Form", "OCR识别套打---VX:15877159321"))
    self.label_13.setText(_translate("Form", "标志号:"))
    self.label_14.setText(_translate("Form", "保单号:"))
    self.label.setText(_translate("Form", "被保人:"))
    self.label_11.setText(_translate("Form", "代收车船税:"))
    self.label_2.setText(_translate("Form", "身份证:"))
    self.label_4.setText(_translate("Form", "住址:"))
    self.label_6.setText(_translate("Form", "手机号码:"))
    self.label_7.setText(_translate("Form", "车牌号码:"))
    self.label_3.setText(_translate("Form", "厂牌型号:"))
    self.label_8.setText(_translate("Form", "发动机号码:"))
    self.label_5.setText(_translate("Form", "初登日期:"))
    self.label_9.setText(_translate("Form", "车架号码:"))
    self.label_10.setText(_translate("Form", "点击日历选起保日期:"))
    self.pushButton_2.setText(_translate("Form", "保存"))
    self.pushButton_3.setText(_translate("Form", "打印标志"))
    self.pushButton_4.setText(_translate("Form", "打印保单"))
    self.pushButton_5.setText(_translate("Form", "清空"))
    self.labIMG.setText(_translate("Form", "IMG"))
    self.label_15.setText(_translate("Form", "选择套打格式:"))
    self.taodaButton.setText(_translate("Form", "选 择"))
    self.label_18.setText(_translate("Form", "选择识别类型:"))
    self.OCRleixingBT.setCurrentText(_translate("Form", "身份证"))
    self.OCRleixingBT.setItemText(0, _translate("Form", "身份证"))
    self.OCRleixingBT.setItemText(1, _translate("Form", "行驶证"))
    self.OCRleixingBT.setItemText(2, _translate("Form", "驾驶证"))
    self.OCRleixingBT.setItemText(3, _translate("Form", "户口本"))
    self.label_16.setText(_translate("Form", "选择识别类型:"))
    self.OCRleixingBT_2.setCurrentText(_translate("Form", "身份证"))
    self.OCRleixingBT_2.setItemText(0, _translate("Form", "身份证"))
    self.OCRleixingBT_2.setItemText(1, _translate("Form", "行驶证"))
    self.OCRleixingBT_2.setItemText(2, _translate("Form", "驾驶证"))
    self.OCRleixingBT_2.setItemText(3, _translate("Form", "户口本"))
    self.label_17.setText(_translate("Form", "选择识别图像:"))
    self.XZIMG.setText(_translate("Form", "选 择"))
    self.label_19.setText(_translate("Form", "选择识别图像:"))
    self.XZIMG_2.setText(_translate("Form", "选 择"))
    self.setWindowIcon(QIcon(':/favicon.ico'))
def get_file_content(self, filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

def xuanztp(self):
    fileName_choose, filetype = QFileDialog.getOpenFileName(self,
                                                            "选取图片文件",
                                                            self.GetDesktopPath,  # 起始路径
                                                            "图像文件 (*.jpg *.png)")  # 设置文件扩展名过滤,用双分号间隔

    if fileName_choose == "":
        self.print("\n取消选择")
        return

    self.print("\n你选择的文件为:")
    self.print(fileName_choose)
    print("文件筛选器类型: ", filetype)
    self.filePath = fileName_choose
    '''图片显示在标签上'''
    self.labIMG.setPixmap(QPixmap(fileName_choose))
    '''让图片适应标签的面积'''
    self.labIMG.setScaledContents(True)
    tj = os.path.getsize(fileName_choose)
    print(tj)
    #self.plainTextEdit_IMG.setPlainText(fileName_choose)
    self.typeTp()
def xuanztp11(self):
    fileName_choose, filetype = QFileDialog.getOpenFileName(self,
                                                            "选取图片文件",
                                                            self.GetDesktopPath,  # 起始路径
                                                            "图像文件 (*.jpg *.png)")  # 设置文件扩展名过滤,用双分号间隔

    if fileName_choose == "":
        self.print("\n取消选择")
        return

    self.print("\n你选择的文件为:")
    self.print(fileName_choose)
    print("文件筛选器类型: ", filetype)
    self.filePath = fileName_choose
    '''图片显示在标签上'''
    self.labIMG.setPixmap(QPixmap(fileName_choose))
    '''让图片适应标签的面积'''
    self.labIMG.setScaledContents(True)
    tj = os.path.getsize(fileName_choose)
    print(tj)
    #self.plainTextEdit_IMG.setPlainText(fileName_choose)
    self.typeTp1()
def typeTp(self):
    if self.OCRleixingBT.currentIndex()==0:
        self.print('识别身份证')
        self.IDOCR()
    elif self.OCRleixingBT.currentIndex()==1:
        self.print('识别行驶')
        self.XSZOCR()
    elif self.OCRleixingBT.currentIndex()==2:
        self.print('识别驾驶')
        self.baidu_reJSZ()
    elif self.OCRleixingBT.currentIndex()==3:
        self.print('识别户口本')
        self.print('识别户口本功能暂未完成')
def typeTp1(self):
    if self.OCRleixingBT_2.currentIndex()==0:
        self.print('识别身份证')
        self.IDOCR()
    elif self.OCRleixingBT_2.currentIndex()==1:
        self.print('识别行驶')
        self.XSZOCR()
    elif self.OCRleixingBT_2.currentIndex()==2:
        self.print('识别驾驶')
        self.baidu_reJSZ()
    elif self.OCRleixingBT_2.currentIndex()==3:
        self.print('识别户口本')
        self.print('识别户口本功能暂未完成')
def xuanztp1(self):
    fileName_choose1, filetype = QFileDialog.getOpenFileName(self,
                                                             "选取图片文件",
                                                             self.GetDesktopPath,  # 起始路径
                                                             "图像文件 (*.jpg *.png)")  # 设置文件扩展名过滤,用双分号间隔

    if fileName_choose1 == "":
        self.print("\n取消选择")
        return

    self.print("\n你选择的文件为:")
    self.print(fileName_choose1)
    print("文件筛选器类型: ", filetype)
    self.filePath1 = fileName_choose1
    self.xszimg.setPixmap(QPixmap(fileName_choose1))
    self.xszimg.setScaledContents(True)
def xuanztp2(self):
    try:
        fileName_choose2, filetype = QFileDialog.getOpenFileName(self,
                                                                 "选取Execl文件",
                                                                 self.GetDesktopPath,  # 起始路径
                                                                 "Execl文件(*.xlsx *.xls")  # 设置文件扩展名过滤,用双分号间隔

        if fileName_choose2 == "":
            self.print("\n取消选择")
            return

        self.print("\n你选择的文件为:")
        self.print(fileName_choose2)
        print("文件筛选器类型: ", filetype)

        self.taodainput.setPlainText(fileName_choose2)

        inilujing = os.access(self.GetDesktopPath + '\\lujing.ini', False)
        print(inilujing)
        if inilujing == True:
            os.remove(self.GetDesktopPath + '\\lujing.ini')#删除旧文件
        #  实例化configParser对象
        config = configparser.ConfigParser()
        # -read读取ini文件
        config.read(self.GetDesktopPath+'\\lujing.ini', encoding='GB18030')
        list = []
        list = config.sections()  # 获取到配置文件中所有分组名称

        if 'type' not in list:  # 如果分组type不存在则插入type分组
            config.add_section('type')
            config.set('type', 'lujing', fileName_choose2)  # 给type分组设置值

        o = open(self.GetDesktopPath+'\\lujing.ini', 'w')
        config.write(o)
        o.close()  # 不要忘记关闭

        self.ini_chushihua()
    except Exception as e:
        self.print(e)

def IDOCR(self):
    self.print("识别身份证")
    '''判断路径文件是否存在'''
    SD = os.access(self.filePath, False)
    if SD == True:
        text = self.baidu_regID()
        # text = ''.join(text)
        # print(type(text))
        print(text)
        try:
            if text is None:
                print("识别失败")
                self.print('识别失败!请确认图片是否清晰,类型是不是身份证')
                self.plainTextEdit_2.setPlainText('')
                self.plainTextEdit_4.setPlainText('')
            else:

                self.print("识别成功")

                self.plainTextEdit.setPlainText(text[0])
                self.plainTextEdit_2.setPlainText(text[1])
                self.plainTextEdit_4.setPlainText(text[2])

        except Exception as e:
            self.print(e)
    else:
        self.print("识别失败")
        self.print('识别失败!小工具找不到图片!请重新选择图片!')
        self.plainTextEdit_2.setPlainText('')
        self.plainTextEdit_4.setPlainText('')

def XSZOCR(self):
    try:
        '''判断路径文件是否存在'''
        SD = os.access(self.filePath, False)
        if SD == True:
            text = self.baidu_regXSZ()
            # text = ''.join(text)
            # print(type(text))
            print(text)

            if text is None:
                print("识别失败")
                self.print('识别失败!请确认图片是否清晰,类型是不是行驶证')
                self.plainTextEdit_3.setPlainText('')
                self.plainTextEdit_5.setPlainText('')
                self.plainTextEdit_7.setPlainText('')
                self.plainTextEdit_8.setPlainText('')
                self.plainTextEdit_9.setPlainText('')
            else:
                self.print("识别成功")

                self.plainTextEdit_3.setPlainText(text[2])#厂牌
                nian = (text[7][0:4])
                yue = (text[7][4:6])
                ri = (text[7][6:8])
                self.plainTextEdit_5.setPlainText(nian+'-'+yue+'-'+ri)#初登日期
                self.plainTextEdit_7.setPlainText(text[6])#车牌
                self.plainTextEdit_8.setPlainText(text[5])#发动机
                self.plainTextEdit_9.setPlainText(text[0])#车架号
        else:
            print("识别失败")
            self.print('识别失败!小工具找不到图片!请重新选择图片!')
            self.plainTextEdit_3.setPlainText('')
            self.plainTextEdit_5.setPlainText('')
            self.plainTextEdit_7.setPlainText('')
            self.plainTextEdit_8.setPlainText('')
            self.plainTextEdit_9.setPlainText('')
    except Exception as e:
        self.print(e)

def baidu_regID(self):
        options = {}
        options["detect_direction"] = "true"
        options["detect_risk"] = "false"
        options['id_card_side'] = 'front'
        image = self.get_file_content(self.filePath)
        result = self.client.idcard(image, 'back',options)  # 上传图片获得请求识别结果
        print(result)
        self.print('身份证元素有{}个'.format(len(result["words_result"])))

        if len(result["words_result"]) > 3:

            # print("姓名:", result["words_result"]["姓名"]["words"])
            # print("性别:", result["words_result"]["性别"]["words"])
            # print("民族:", result["words_result"]["民族"]["words"])
            # print("生日:", result["words_result"]["出生"]["words"])
            # print("身份证号:", result["words_result"]["公民身份号码"]["words"])
            # print("住址:", result["words_result"]["住址"]["words"])

            data = []
            data.append(result["words_result"]["姓名"]["words"])
            data.append(result["words_result"]["公民身份号码"]["words"])
            data.append(result["words_result"]["住址"]["words"])
            return data
        else:
            print('识别失败')

def baidu_regXSZ(self):
    image = self.get_file_content(self.filePath)
    options = {}
    options["detect_direction"] = "true"
    options["accuracy"] = "normal"
    result = self.client.vehicleLicense(image, options)  # vehicleLicense行驶证识别
    try:
        if result['words_result_num'] == 10:  # 以元素个数来判断是否识别成功
            print(result)
            data1 = []
            data1.append(result["words_result"]["车辆识别代号"]["words"])
            #data1.append(result["words_result"]["住址"]["words"])
            data1.append(result["words_result"]["发证日期"]["words"])
            data1.append(result["words_result"]["品牌型号"]["words"])
            data1.append(result["words_result"]["车辆类型"]["words"])
            #data1.append(result["words_result"]["所有人"]["words"])
            data1.append(result["words_result"]["使用性质"]["words"])
            data1.append(result["words_result"]["发动机号码"]["words"])
            data1.append(result["words_result"]["号牌号码"]["words"])
            data1.append(result["words_result"]["注册日期"]["words"])
            return data1
    except Exception as e:
        self.print('请求参数返回没有{},识别失败'.format(e))

def baidu_reJSZ(self):
    '''判断路径文件是否存在'''
    SD = os.access(self.filePath, False)
    if SD == True:
        image = self.get_file_content(self.filePath)
        options = {}
        options["detect_direction"] = "true"
        result = self.client.drivingLicense(image, options)  # vehicleLicense行驶证识别
        try:
            if result['words_result_num'] == 10:  # 以元素个数来判断是否识别成功
                print(result)
                data1 = []
                data1.append(result["words_result"]["姓名"]["words"])
                data1.append(result["words_result"]["证号"]["words"])
                data1.append(result["words_result"]["住址"]["words"])
                # data1.append(result["words_result"]["品牌型号"]["words"])
                # data1.append(result["words_result"]["车辆类型"]["words"])
                # # data1.append(result["words_result"]["所有人"]["words"])
                # data1.append(result["words_result"]["使用性质"]["words"])
                # data1.append(result["words_result"]["发动机号码"]["words"])
                # data1.append(result["words_result"]["号牌号码"]["words"])
                # data1.append(result["words_result"]["注册日期"]["words"])
                for i in data1:
                    print(i)
                self.print("识别成功")

                self.plainTextEdit.setPlainText(data1[0])
                self.plainTextEdit_2.setPlainText(data1[1])
                self.plainTextEdit_4.setPlainText(data1[2])

                return data1
            else:
                self.print('驾驶证识别失败')
        except Exception as e:
            self.print('请求参数返回没有{},识别失败'.format(e))

def print(self,text):
    self.textEdit.append(str(text+'\n'))

if name == ‘main’:

app = QtWidgets.QApplication(sys.argv)
'''实例化窗体'''
mainForm = MainForm('测试QFileDialog')
'''实例化窗体代码'''
mainForm.setupUi(mainForm)
'''显示'''
mainForm.show()
sys.exit(app.exec_())

你可能感兴趣的:(学习,python,ocr,excel)