ocr:img转成pdf,pdf转成pdf

Posted on 2019-08-22 15:36  大白不白  阅读(587)  评论(0编辑  收藏  举报
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
import os,win32process,win32event
import re,pymysql,uuid


class Resquest(BaseHTTPRequestHandler):
    def do_GET(self):
        print(self.requestline)
        if self.path != '/hello':
            self.send_error(404, "Page not Found!")
            return

    def do_POST(self):
        # print(self.headers)
        data = str(self.headers)
        data_type = data.split("\n")
        res_type = data_type[0].split(":")[1]
        print(res_type)
        uuid_str = uuid.uuid4().hex
        req_datas = self.rfile.read(int(self.headers['content-length']))
        pattern1 = re.compile(b"-+\w+\s{2}(.*?\s{2}){2}\s{2}")
        pattern2 = re.compile(b"\s{2}-+\w+-+\s{2}")
        res1 = re.match(pattern1, req_datas)
        res2 = re.search(pattern2, req_datas)
        file_data = req_datas[res1.end():res2.start()]
        if res_type == " jpg":
            with open(r"C:\Users\Administrator\Desktop\ocr\data\jpg\%s.jpg"%uuid_str, "wb") as w:
                w.write(file_data)
            # 9个参数,第二个参数输入命令
            handle = win32process.CreateProcess(None,"C:\\Users\\Administrator\\Desktop\\ocr\\img2pdf.exe C:\\Users\\Administrator\\Desktop\\ocr\\data\\jpg\\%s.jpg C:\\Users\\Administrator\\Desktop\\ocr\\data\\jpg\\%s.pdf"%(uuid_str,uuid_str),None, None, 0, 0, None, None, win32process.STARTUPINFO())
            # 等待进程执行完,返回0
            flag = win32event.WaitForSingleObject(handle[0],300000)
            # print(handle[0])
            print(flag)
            db = pymysql.connect("127.0.0.1", "root", "123", "ocr")
            cursor = db.cursor()
            sql = "INSERT INTO ocr(name,path) VALUES('%s','C:\\Users\\Administrator\\Desktop\\ocr\\data\\jpg\\%s.pdf')"%(uuid_str,uuid_str)
            cursor.execute(sql)
            db.commit()  # 提交数据
            cursor.close()
            db.close()

            if flag!=0:
                # 杀死进程
                win32process.TerminateProcess(handle[0],0)

            if os.path.exists(r"C:\\Users\\Administrator\\Desktop\\ocr\\data\\jpg\\%s.txt"%uuid_str):
                with open("C:\\Users\\Administrator\\Desktop\\ocr\\data\\jpg\\%s.txt"%uuid_str,"r",encoding="utf8")as f:
                    res = f.read()
                    print(res)
                    data = {
                        'result_code': '1',
                        'result_desc': 'Success',
                        'file_data': res,
                    }
                    self.send_response(200)
                    self.send_header('Content-type', 'application/json')
                    self.end_headers()
                    self.wfile.write(json.dumps(data).encode("utf-8"))


        elif res_type == " pdf":
            # print(req_datas)
            with open(r"C:\Users\Administrator\Desktop\ocr\data\pdf\xx.pdf", "wb") as w:
                w.write(req_datas)
            handle = win32process.CreateProcess(None,"C:\\Users\\Administrator\\Desktop\\ocr\\pdf2spdf.exe C:\\Users\\Administrator\\Desktop\\ocr\\data\\pdf\\xx.pdf C:\\Users\\Administrator\\Desktop\\ocr\\data\\pdf\\%s.pdf"%(uuid_str),None, None, 0, 0, None, None, win32process.STARTUPINFO())
            flag = win32event.WaitForSingleObject(handle[0], 300000)

            db = pymysql.connect("127.0.0.1", "root", "123", "ocr")
            cursor = db.cursor()
            sqq = "INSERT INTO ocr(name,path) VALUES('%s','C:\\Users\\Administrator\\Desktop\\ocr\\data\\pdf\\%s.pdf')"%(uuid_str,uuid_str)
            cursor.execute(sqq)
            db.commit()  # 提交数据
            cursor.close()
            db.close()

            if flag!=0:
                win32process.TerminateProcess(handle[0],0)

            if os.path.exists(r"C:\\Users\\Administrator\\Desktop\\ocr\\data\\pdf\\%s_.txt"%uuid_str):
                with open("C:\\Users\\Administrator\\Desktop\\ocr\\data\\pdf\\%s_.txt"%uuid_str,"r",encoding="utf8")as f:
                    res = f.read()
                    print("44")
                    print(res)
                    data = {
                        'result_code': '2',
                        'result_desc': 'Success',
                        'file_data': res,
                    }
                    self.send_response(200)
                    self.send_header('Content-type', 'application/json')
                    self.end_headers()
                    self.wfile.write(json.dumps(data).encode('utf-8'))
            else:
                return None
        else:
            return "error"


if __name__ == '__main__':
    host = ('127.0.0.1', 9002)
    server = HTTPServer(host, Resquest)
    print("Starting server, listen at: %s:%s" % host)
    server.serve_forever()

 

Copyright © 2024 大白不白
Powered by .NET 8.0 on Kubernetes