convert.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. sys.getdefaultencoding()
  4. reload(sys)
  5. sys.setdefaultencoding('UTF-8')
  6. from demeter.core import *
  7. import os
  8. import os.path
  9. import requests
  10. import shutil
  11. # 测试命令行传参 python convert.py -f file.doc
  12. param = {}
  13. param['file'] = 'f'
  14. Demeter.getopt(param)
  15. def command(file):
  16. id = int(file)
  17. model = Demeter.model('convert')
  18. model.id = id
  19. info = convert.select(type='fetchone')
  20. status = True
  21. if info['status'] == 1 or info['status'] == 4:
  22. status = False
  23. if info and status == False:
  24. model.id = id
  25. update = {}
  26. update['status'] = 2
  27. #model.update(update)
  28. if not File.exists(info['local']):
  29. if 'http' in info['file']:
  30. r = requests.get(info['file'], stream=True)
  31. with open(info['local'], 'wb') as up:
  32. for chunk in r.iter_content(chunk_size=1024):
  33. if chunk:
  34. up.write(chunk)
  35. else:
  36. shutil.copyfile(info['file'], info['local'])
  37. if not File.exists(info['html']):
  38. convert = 'cd ' + info['path'] + ' && '
  39. convert = convert + 'libreoffice --invisible --convert-to pdf ' + info['local']
  40. convert = convert + ' && '
  41. convert = convert + 'pdf2htmlEX --zoom 1.3 --no-drm 1 --split-pages 1 '
  42. convert = convert + '--embed-css 1 --embed-javascript 0 --embed-image 0 --embed-font 1 '
  43. convert = convert + '--embed-external-font 0 --dest-dir '+info['path']+' --page-filename %d.page ' + info['pdf']
  44. print convert
  45. return
  46. Shell.popen(convert)
  47. if File.exists(info['html']):
  48. # 获取有多少页
  49. page = total(info['path'])
  50. model.id = id
  51. update = {}
  52. update['page'] = page
  53. update['status'] = 3
  54. model.update(update)
  55. return
  56. model.id = id
  57. update = {}
  58. update['status'] = 4
  59. model.update(update)
  60. def total(path):
  61. page = 0
  62. for parentdir,dirname,filenames in os.walk(path):
  63. for filename in filenames:
  64. if os.path.splitext(filename)[1]=='.page':
  65. page = page + 1
  66. return page
  67. file = Demeter.option['file']
  68. command(file)