import json import os import sys from pprint import pprint import bottle from bottle import redirect from bottle import request from bottle import response from bottle import route, run from bottle import template from bottle import error import config from filepath2text import route_filename from utils import get_dir_next_num bottle.BaseRequest.MEMFILE_MAX = 8 * 1024 * 1024 # bytes save_path = '../upload' if not os.path.isdir(save_path): os.mkdir(save_path) file_next_num = get_dir_next_num(save_path, which="files") def start_word2html_app(kill_mathtype=True): if kill_mathtype: os.system("taskkill /f /im MathType.exe") os.system("taskkill /f /im WINWORD.EXE") os.system("taskkill /f /im ConsoleApplication1.exe") os.system("start {}".format(config.word2html_exe)) # start 在新窗口中打开 def set_response_header(response): response.set_header("Access-Control-Allow-Headers", "x-requested-with,content-type,Authorization") response.set_header("Access-Control-Allow-Methods", "POST,GET,OPTIONS") response.set_header("Access-Control-Allow-Origin", "*") @route('/', method='GET') @route('/', method='POST') @route('/', method='OPTIONS') def index(): set_response_header(response) return redirect("/ocr_page") @route('/ocr_page', method='GET') @route('/ocr_page', method='POST') @route('/ocr_page', method='OPTIONS') def ocr_page(): set_response_header(response) return template("index.html") @route('/ocr', method='GET') @route('/ocr', method='POST') @route('/ocr', method='OPTIONS') def do_ocr(): global file_next_num set_response_header(response) upload = request.files.get('mydata') if upload: print("upload file: {}".format(upload.raw_filename)) num_str = str(file_next_num) file_next_num += 1 cur_save_path = save_path upload.raw_filename = num_str + os.path.splitext(upload.raw_filename)[1] upload.save(cur_save_path, overwrite=True) # 把文件保存到save_path路径下 filename = os.path.join(cur_save_path, upload.raw_filename) filename = os.path.abspath(filename) try: text = route_filename(filename) # pprint(text) if text: status = "OK" print("parse is over\n") else: status = "ERROR" text = "无法解析文件" # print([text]) # pprint([text]) except: status = "ERROR" text = "无法解析文件" else: status = "ERROR" text = "request.files.get('mydata') is None, 请检查文件名是否包含中文" res = {"status": status, "text": text} return json.dumps(res, ensure_ascii=False) if __name__ == "__main__": start_word2html_app() if len(sys.argv) == 1: # 默认在本地运行 ip, port = config.LocalCfg.ip, config.LocalCfg.port run(host=ip, port=port, server='tornado') else: if sys.argv[1] == 'local': ip, port = config.LocalCfg.ip, config.LocalCfg.port run(host=ip, port=port, server='tornado') elif sys.argv[1] == 'testing': ip, port = config.TestingCfg.ip, config.TestingCfg.port run(host=ip, port=port, server='tornado') elif sys.argv[1] == 'production': ip, port = config.ProductionCfg.ip, config.ProductionCfg.port # run(host=ip, port=port, server='tornado', debug=True, reloader=True) run(host=ip, port=port, debug=True) else: print('cmd should be: python server.py local') print('or: python server.py testing') print('or: python server.py production')