format.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #coding=utf-8
  2. import datetime
  3. import traceback
  4. import tablib
  5. import base64
  6. from datetime import timedelta
  7. from docx import Document
  8. from docx.oxml.ns import qn
  9. from openpyxl.utils.exceptions import InvalidFileException
  10. def strfdate(d):
  11. if d:
  12. return d.strftime('%Y-%m-%d')
  13. else:
  14. return ''
  15. def strftime(t):
  16. if t:
  17. return t.strftime('%Y-%m-%d %H:%M')
  18. else:
  19. return ''
  20. def strfsecond(second):
  21. sec = timedelta(seconds=second)
  22. d = datetime.datetime(1,1,1) + sec
  23. if d.hour > 0:
  24. if d.minute > 0:
  25. if d.second > 0:
  26. retval = "%d小时%d分钟%d秒" % (d.hour, d.minute, d.second)
  27. else:
  28. retval = "%d小时%d分钟" % (d.hour, d.minute)
  29. else:
  30. if d.second > 0:
  31. retval = "%d小时%d秒" % (d.hour, d.second)
  32. else:
  33. retval = "%d小时" % (d.hour)
  34. else:
  35. if d.minute > 0:
  36. if d.second > 0:
  37. retval = "%d分钟%d秒" % (d.minute, d.second)
  38. else:
  39. retval = "%d分钟" % (d.minute)
  40. else:
  41. retval = "%d秒" % (d.second)
  42. return retval
  43. def clean_datetime_range(data, fieldname):
  44. if data is not None and fieldname in data and data[fieldname] != '':
  45. t = data[fieldname].split(' - ')
  46. data = data.copy()
  47. data[fieldname+'_after'] = t[0]
  48. data[fieldname+'_before'] = t[1] + ' 23:59:59'
  49. data.pop(fieldname)
  50. return data
  51. class ExcelImporter():
  52. @staticmethod
  53. def validity(file):
  54. status = {
  55. 'success': True,
  56. 'errors': '',
  57. 'data': []
  58. }
  59. if file == None:
  60. status['success'] = False
  61. status['errors'] = u"请上传数据文件"
  62. else:
  63. try:
  64. data = tablib.import_set(file, format='xlsx').dict
  65. if len(data) == 0:
  66. status['success'] = False
  67. status['errors'] = u"上传的文件内没有发现数据"
  68. else:
  69. status['data'] = data
  70. except InvalidFileException:
  71. status['success'] = False
  72. status['errors'] = u"请上传<strong>xlsx</strong>格式的数据文件,老版本的xls格式不被支持!"
  73. except Exception as e:
  74. traceback.print_exc()
  75. status['success'] = False
  76. status['errors'] = u"导入失败"
  77. return status
  78. class WordImporter():
  79. @staticmethod
  80. def parse(file):
  81. doc = Document(file)
  82. question_data = []
  83. question = {}
  84. is_option = False
  85. is_image = False
  86. for para in doc.paragraphs:
  87. text = para.text.strip()
  88. if text:
  89. if text.startswith("【序号】"):
  90. question = {}
  91. is_option = False
  92. is_image = False
  93. question_data.append(question)
  94. question['num'] = text.split("【序号】")[-1].strip()
  95. elif text.startswith("【题文】"):
  96. question['title'] = text.split("【题文】")[-1].strip()
  97. elif text.startswith("【图片】"):
  98. is_image = True
  99. question['images'] = []
  100. elif text.startswith("【章节】"):
  101. question['chapter'] = text.split("【章节】")[-1].strip()
  102. elif text.startswith("【题型】"):
  103. #print("====>", text)
  104. question['type'] = text.split("【题型】")[-1].strip()
  105. elif text.startswith("【答案】"):
  106. question['answer'] = text.split("【答案】")[-1].strip()
  107. elif text.startswith("【解析】"):
  108. question['analysis'] = text.split("【解析】")[-1].strip()
  109. elif text.startswith("【难度】"):
  110. question['difficulty'] = text.split("【难度】")[-1].strip()
  111. elif text.startswith("【分数】"):
  112. question['score'] = text.split("【分数】")[-1].strip()
  113. elif text.startswith("【选项】"):
  114. is_option = True
  115. question['options'] = []
  116. elif is_option and text:
  117. question['options'].append(text)
  118. elif len(para.runs) > 0 and is_image:
  119. image_data = WordImporter.extract_image(para)
  120. image_data = base64.b64encode(image_data).decode('utf-8')
  121. question['images'].append(image_data)
  122. #print(question_data)
  123. return question_data
  124. @staticmethod
  125. def extract_image(para):
  126. for run in para.runs:
  127. if run.element.xpath('.//a:blip'):
  128. blip = run.element.xpath('.//a:blip')[0]
  129. embed = blip.get(qn('r:embed'))
  130. related_part = run.part.related_parts[embed]
  131. image_bytes = related_part.blob
  132. return image_bytes
  133. return None