parsedocx.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #-*-coding:utf-8 -*-
  2. import re,os
  3. import json,uuid
  4. from bs4 import BeautifulSoup
  5. from win32com.client import Dispatch
  6. class DocxConverter(object):
  7. """
  8. """
  9. def __init__(self,docpath=None):
  10. """
  11. """
  12. self.docpath = docpath
  13. self.word = Dispatch("Word.Application")
  14. self.word.Visible = 0
  15. self.doc = word.Documents.Open(self.docpath)
  16. def docx2html(self):
  17. """
  18. """
  19. html = os.path.join(os.path.dirname(self.docpath),str(uuid.uuid4())+".html")
  20. self.doc.SaveAs(html)
  21. self.doc.Close()
  22. self.word.Quit()
  23. return html
  24. class QuestionsParser(object):
  25. """试题解析
  26. """
  27. def __init__(self,name="test4.html"):
  28. self.html = open(name,"r").read()
  29. self.soup = BeautifulSoup(self.html,"html.parser")
  30. def get_paragraphs(self):
  31. """
  32. """
  33. wordsection = self.soup.find("div",class_="WordSection1")
  34. #print wordsection
  35. pars = wordsection.find_all("p")
  36. return pars
  37. def parse_questions(self):
  38. """提取试题
  39. """
  40. que_type_dct = {}
  41. paragraphs = self.get_paragraphs()
  42. for i,p in enumerate(paragraphs):
  43. print p.text
  44. if u"【题型】" in p.text:
  45. que_type_dct["type"] = p.text.split("、")[-1]
  46. def parse_questions(self):
  47. """提取试题
  48. """
  49. data = []
  50. tmp_val = {}
  51. tx_name = ""
  52. key = ""
  53. paragraphs = self.get_paragraphs()
  54. for i,p in enumerate(paragraphs):
  55. if u"【题型】" in p.text:
  56. tx_name = p.text
  57. if u"【题干】" in p.text:
  58. key = "tg"
  59. tmp_val["tx"] = tx_name
  60. if tmp_val.get("tg"):
  61. data.append(tmp_val)
  62. tmp_val = {"tg":"","tx":"","zsd":"","nd":"","da":"","jx":""}
  63. if u"【知识点】" in p.text:
  64. key = "zsd"
  65. if u"【难度】" in p.text:
  66. key = "nd"
  67. if u"【答案】" in p.text:
  68. key = "da"
  69. if u"【解析】" in p.text:
  70. key = "jx"
  71. if key != "":
  72. tmp_val[key] += p.__str__()
  73. data.append(tmp_val)
  74. return data
  75. def get_questions(self):
  76. """
  77. """
  78. questions = self.parse_questions()
  79. for que in questions:
  80. que["tx"] = que["tx"].split(u"、")[-1]
  81. #que["tg"] = que["tg"].replace(u"【题干】","")
  82. #que["zsd"] = que["zsd"].replace(u"【知识点】","")
  83. #que["da"] = que["da"].replace(u"【答案】","")
  84. #que["jx"] = que["jx"].replace(u"【解析】","")
  85. que["qno"] = self.get_qno(que["tg"])
  86. return questions
  87. def get_qno(self,tg):
  88. """提取题号
  89. """
  90. tgsoup = BeautifulSoup(tg,"html.parser")
  91. tgtext = tgsoup.text
  92. qno = re.search(r"\d+",tgtext.split(u"、")[0]).group()
  93. return qno
  94. questionparser = QuestionsParser()
  95. if __name__ == "__main__":
  96. ques = questionparser.get_questions()
  97. with open("t.json","w+") as f:
  98. f.write(json.dumps(ques))