ocr:img转成pdf,pdf转成pdf

假装没事ソ 提交于 2019-11-28 05:39:43
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
import subprocess
import time
import os
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)
        print(self.command)
        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)
            subprocess.Popen("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), shell=True)
            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()
            time.sleep(1)
            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()
                    self.wfile.write(json.dumps(res).encode("utf-8"))
            data = {
                'result_code': '1',
                'result_desc': 'Success',
                'file': "C:\\Users\\Administrator\\Desktop\\ocr\\data\\jpg\\%s.pdf"%uuid_str,
            }
            self.send_response(200)
            self.send_header('Content-length', '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)
            subprocess.Popen("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),shell=True)
            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()
            time.sleep(1)
            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()
                    self.wfile.write(json.dumps(res).encode("utf-8"))

            data = {
                'result_code': '2',
                'result_desc': 'Success',
                'file': "C:\\Users\\Administrator\\Desktop\\ocr\\data\\pdf\\%s.pdf"%uuid_str,
            }
            # print(json.dumps(data).encode('utf-8'))
            self.send_response(200)
            self.send_header('Content-length', 'application/json')
            self.end_headers()
            self.wfile.write(json.dumps(data).encode('utf-8'))

        else:
            return


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

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!