# -*- coding: utf-8 -*- import sys sys.getdefaultencoding() reload(sys) sys.setdefaultencoding('UTF-8') from demeter.core import * import os import os.path import requests import shutil # 测试命令行传参 python convert.py -f file.doc param = {} param['file'] = 'f' Demeter.getopt(param) def command(file): id = int(file) model = Demeter.model('convert') model.id = id info = convert.select(type='fetchone') status = True if info['status'] == 1 or info['status'] == 4: status = False if info and status == False: model.id = id update = {} update['status'] = 2 #model.update(update) if not File.exists(info['local']): if 'http' in info['file']: r = requests.get(info['file'], stream=True) with open(info['local'], 'wb') as up: for chunk in r.iter_content(chunk_size=1024): if chunk: up.write(chunk) else: shutil.copyfile(info['file'], info['local']) if not File.exists(info['html']): convert = 'cd ' + info['path'] + ' && ' convert = convert + 'libreoffice --invisible --convert-to pdf ' + info['local'] convert = convert + ' && ' convert = convert + 'pdf2htmlEX --zoom 1.3 --no-drm 1 --split-pages 1 ' convert = convert + '--embed-css 1 --embed-javascript 0 --embed-image 0 --embed-font 1 ' convert = convert + '--embed-external-font 0 --dest-dir '+info['path']+' --page-filename %d.page ' + info['pdf'] print convert return Shell.popen(convert) if File.exists(info['html']): # 获取有多少页 page = total(info['path']) model.id = id update = {} update['page'] = page update['status'] = 3 model.update(update) return model.id = id update = {} update['status'] = 4 model.update(update) def total(path): page = 0 for parentdir,dirname,filenames in os.walk(path): for filename in filenames: if os.path.splitext(filename)[1]=='.page': page = page + 1 return page file = Demeter.option['file'] command(file)