import cv2 import cv2.cv as cv import codecs import numpy as np import tesseract import glob import time import os import re import xlrd import sys import datetime import xml from xml.dom.minidom import Document import xml.etree.ElementTree as ET def create_report(): """ crating an xml report """ langue = 'french' doc = Document() top = doc.createElement("rapport_test") doc.appendChild(top) liste = str(datetime.datetime.now()).split('.') liste1 = liste[0] timee1 = liste[0].replace(' ','-') time_date = liste[0].split(' ') date_att = time_date[0] temps_att = time_date[1] timee=timee1.split('-') permeute = timee[0] timee[0] = timee[2] timee[2] = permeute temps = '-'.join(timee) time2 = temps.split(':') del time2[-1] temps = '-'.join(time2) top.setAttribute('date',date_att) top.setAttribute('time',temps_att) top.setAttribute('nb_correcte','') top.setAttribute('nb_incorrecte','') top.setAttribute('langue',langue) xml_file = open("essai_rapport_test_"+temps+"_"+langue+".xml","w") xml_file.write(doc.toprettyxml()) xml_file.close() #----------------------------------------------------------------------------------------------------------------------------------------------- #----------------------------------------------------------------------------------------------------------------------------------------------- #----------------------------------------------------------------------------------------------------------------------------------------------- #----------------------------------------------------------------------------------------------------------------------------------------------- def modify_report(msg,icon,numero_alerte): """ modifying the created report """ wb = xlrd.open_workbook(filename = 'source.xlsx') ws = wb.sheet_by_index(1) file_name = max(glob.iglob('essai_rapport_test*.xml'),key=os.path.getctime) tree = ET.parse(file_name) top = tree.getroot() for i in range(ws.nrows): if i != 0: #si on trouve l'alerte dont le numero est numero_alerte if ws.cell_value(i,0) == numero_alerte: #création d'un élément et ajout du numero_alerte,message d'alerte et icone d'alerte à partie de la base alerte = ET.SubElement(top,"alerte") alerte.set("anumero_alerte",str(numero_alerte)) alerte.set("bmessage_alerte",ws.cell_value(i,3)) alerte.set("cicone_alerte",ws.cell_value(i,2)) print "bytes" print bytes(msg) alerte.set("dmessage_trouve",unicode(msg,"UTF-8")) alerte.set("eicone_trouve",unicode(icon)) alerte.set("gresmsg",'OK') alerte.set("hresicon",'OK') alerte.set("fresultat",'OK') nb = 1 break file1= open(file_name,"a") tree.write(file1) file1.close() def add_pi(): """ adding processing instruction""" file_name = max(glob.iglob('essai_rapport_test*.xml'),key=os.path.getctime) f = open(file_name,'r') old = f.readlines() f.close() old.insert(1,'\n') new1 = "".join(old) f = open(file_name,'w') f.write(new1) f.close() def delete_rep(): """ deleting useless line""" file_name = max(glob.iglob('essai_rapport_test*.xml'),key=os.path.getctime) f = open(file_name,'r') old = f.readlines() f.close() del old[2] new1 = "".join(old) f = open(file_name,'w') f.write(new1) f.close() #------------------------------------------------------------------------------------------------------------------------------------------------------------------- def traite(): api = tesseract.TessBaseAPI() api.Init(".","fra",tesseract.OEM_DEFAULT) api.SetPageSegMode(tesseract.PSM_AUTO) api.SetPageSegMode(tesseract.PSM_AUTO) mImgFile = "text_gen0.jpg" result = tesseract.ProcessPagesFileStream(mImgFile,api) strr = result.split("\n") result1 = ' '.join(strr) print result1 add_pi() modify_report(result,'ICO_STOP.bmp',5) delete_rep() #create_report() #traite()