def collect_topic_to_dirs(self, topic: TopicElement, parent_dir) -> List[TopicDir]: """将主题收集为目录名""" current_dir = os.path.join(parent_dir, topic.getTitle()) dirs = [TopicDir(topic, current_dir)] for topic in topic.getSubTopics(): dirs.extend(self.collect_topic_to_dirs(topic, current_dir)) return dirs
def collection_lines_as_indent(self, lines: List[str], topic: TopicElement, indent=''): lines.append(indent + topic.getTitle()) indent += ' ' * 4 for i in topic.getSubTopics(): self.collection_lines_as_indent(lines, i, indent)
def creatXmindFile(data): module=[] for item in data: module.append(item['module']) module=list(set(module)) print(module) w = xmind.load("test3.xmind") # load an existing file or create a new workbook if nothing is found s2=w.createSheet() # create a new sheet s2.setTitle("框架") r2=s2.getRootTopic() r2.setTitle("框架") for i in range(len(module)): t=TopicElement() t.setTitle(module[i]) r2.addSubTopic(t) w.addSheet(s2) # the second sheet is now added to the workbook r2_topics=r2.getSubTopics() # to loop on the subTopics for topic in r2_topics: topic_name=topic.getTitle() print (topic_name) for item in data: if topic_name == item['module'] : index=topic.getIndex() t=TopicElement() content=item['caseId']+" "+'\n'+item['summary'] t.setTitle(content) r2_topics[index].addSubTopic(t) summary=t.getTitle() for item in data: if item['summary'] in summary: t1=TopicElement() content1=item['name'] t1.setTitle(content1) t.addSubTopic(t1) xmind.save(w,"test3.xmind") # and we save
def parseHtml(url: str, name: str) -> dict: req = requests.request(method="get", url=url, timeout=10) url = url if url.endswith("/") else url + "/" html = etree.HTML(req.text) h1 = html.xpath("//h1")[0] workbook = xmind.load("{}.xmind".format(name)) sheet1 = workbook.getPrimarySheet() sheet1.setTitle(name + "-1") rootTopic = sheet1.getRootTopic() rootTopic.setTitle(name) rootTopic = rootTopic.addSubTopic() rootTopic.setTitle(h1.text.strip()) html_content = html.xpath( '//div[@class="content"]/*') #type:etree._Element # subNodes = html_content.xpath("/*") h2 = None h3 = None h4 = None currentNode = None for n in html_content: # type:etree._Element if n.tag == "h2": subTopic = TopicElement(ownerWorkbook=workbook) rootTopic.addSubTopic(subTopic) subTopic.setTitle(str(n.xpath("string(.)"))) subTopic.setURLHyperlink(url + "#" + subTopic.getTitle().strip( ).lower().replace(".", "").replace(" ", "-")) h2 = subTopic currentNode = subTopic elif n.tag == "h3": pTopic = h2 if h2 else rootTopic subTopic = TopicElement(ownerWorkbook=workbook) pTopic.addSubTopic(subTopic) subTopic.setTitle(str(n.xpath("string(.)"))) subTopic.setURLHyperlink(url + "#" + subTopic.getTitle().strip( ).lower().replace(".", "").replace(" ", "-")) h3 = subTopic currentNode = subTopic elif n.tag == "h4": pTopic = h3 if h3 else rootTopic subTopic = TopicElement(ownerWorkbook=workbook) pTopic.addSubTopic(subTopic) subTopic.setTitle(str(n.xpath("string(.)"))) subTopic.setURLHyperlink(url + "#" + subTopic.getTitle().strip( ).lower().replace(".", "").replace(" ", "-")) h4 = subTopic currentNode = subTopic else: content = currentNode.getNotes() content = content if content is not None else "" content = content + "\r\n" + (str(n.xpath("string(.)"))) currentNode.setPlainNotes(content) a_ = n.xpath("./a") if len(a_) > 0: for a in a_: subTopic = TopicElement(ownerWorkbook=workbook) currentNode.addSubTopic(subTopic) subTopic.setTitle(str(a.xpath("string(.)"))) subTopic.setURLHyperlink(str(a.attrib.get('href'))) xmind.save(workbook)