excel转xmind

excel转xmind

日常工作中,经常有xmind转excel或者excel转xmind的需求,以下是代码,凑合用

  1 import xmind
  2 import os
  3 import shutil
  4 import zipfile
  5 from openpyxl import load_workbook
  6 
  7 file_path = os.path.dirname(os.path.realpath(__file__))
  8 
  9 
 10 
 11 class convert:
 12     def extract(self, d_path, f_path):
 13         """
 14         zip解压缩乱码问题处理
 15         :param d_path:
 16         :param f_path:
 17         :return:
 18         """
 19         global zf
 20         # os.chdir(root_path)
 21         root = d_path
 22         if not os.path.exists(root):
 23             os.makedirs(root)
 24         # print('ffff',os.getcwd())
 25         zf = zipfile.ZipFile(f_path,"r")
 26 
 27         for n in zf.infolist():
 28             srcName = n.filename
 29             try:
 30                 decodeName = srcName.encode("cp437").decode("utf-8")
 31             except:
 32                 try:
 33                     decodeName = srcName.encode("cp437").decode("gbk")
 34                 except:
 35                     decodeName = srcName
 36             spiltArr = decodeName.split("/")
 37             path = root
 38             for temp in spiltArr:
 39                 path = os.path.join(path, temp)
 40 
 41             if decodeName.endswith("/"):
 42                 if not os.path.exists(path):
 43                     os.makedirs(path)
 44             else:
 45                 if not os.path.exists(os.path.dirname(path)):
 46                     os.makedirs(os.path.dirname(path))
 47                 f = open(path, "wb")
 48                 f.write(zf.read(srcName))
 49                 f.close()
 50         zf.close()
 51 
 52 
 53     # 修复无法打开xmind文件的问题
 54     def aftertreatment(self, path):
 55 
 56         # 修改名字
 57         retval = os.path.dirname(os.path.abspath(__file__))
 58         folder = os.path.dirname(path)
 59         name = os.path.basename(path)
 60         unzip_folder = os.path.splitext(name)[0]
 61         zip_name = unzip_folder + ".zip"
 62         os.chdir(folder)
 63         os.rename(name, zip_name)
 64         os.chdir(retval)
 65         # 解压
 66         unzip_path = os.path.join(folder, unzip_folder)
 67         if not os.path.exists(unzip_path):
 68             os.mkdir(unzip_path)
 69 
 70         inf_folder = os.path.join(unzip_path, "META-INF")
 71         if not os.path.exists(inf_folder):
 72             os.mkdir(inf_folder)
 73 
 74         self.extract(unzip_path, os.path.join(folder, zip_name))
 75         shutil.copyfile("./main_fest.xml", os.path.join(inf_folder, "manifest.xml"))
 76         os.remove(os.path.join(folder, zip_name))
 77         shutil.make_archive(unzip_path, 'zip', unzip_path)
 78         file_path = unzip_path + '.zip'
 79         # print(file_path)
 80         os.chdir(os.path.dirname(file_path))
 81         os.rename(os.path.basename(file_path), name)
 82         os.chdir(retval)
 83         shutil.rmtree(unzip_path)
 84 
 85 
 86     # 转化成xmind文件
 87     def gen_xmind_file(self, xls):
 88         global sub_topic
 89         readbook = load_workbook(xls)
 90         # readbook = xlrd.open_workbook(xls)
 91         # count_sheets = len(readbook.sheets())
 92         # sheet_list = readbook.get_sheet_names()
 93         sheet_list = readbook.sheetnames
 94         count_sheets = len(sheet_list)
 95         try:
 96             for i in range(count_sheets):
 97                 # sheet = readbook.sheet_by_index(i)
 98                 sheet_name = sheet_list[i]
 99                 workbook = xmind.load(sheet_name + '.xmind')
100                 # print(workbook)
101 
102                 first_sheet = workbook.getPrimarySheet()
103                 # 根节点
104                 root_topic = first_sheet.getRootTopic()
105                 root_topic.setTitle(sheet_name)
106                 # sheet = readbook.get_sheet_by_name(sheet_name)
107                 sheet= readbook[sheet_name]
108                 # sheet = readbook.sheetnames(sheet_name)
109 
110                 for row in list(sheet.rows)[1:]:
111                     for c in range(len(row)):
112                         m = 0
113                         value = row[c].value
114                         print(value)
115                         if c == 0:
116                             if len(root_topic.getSubTopics()) == 0:
117                                 sub_topic = root_topic.addSubTopic()
118                                 sub_topic.setTitle(value)
119                             else:
120                                 for j in root_topic.getSubTopics():
121                                     m += 1
122                                     if value == j.getTitle():
123                                         sub_topic = j
124                                         break
125                                     elif m == len(root_topic.getSubTopics()):
126                                         sub_topic = root_topic.addSubTopic()
127                                         sub_topic.setTitle(value)
128                         else:
129                             if len(sub_topic.getSubTopics()) == 0:
130                                 sub_topic = sub_topic.addSubTopic()
131                                 sub_topic.setTitle(value)
132                             else:
133                                 for k in sub_topic.getSubTopics():
134                                     m += 1
135                                     if value == k.getTitle():
136                                         sub_topic = k
137                                         break
138                                     elif m == len(sub_topic.getSubTopics()):
139                                         sub_topic = sub_topic.addSubTopic()
140                                         sub_topic.setTitle(value)
141                     # xmind.save(workbook, path=sheet_name + '.xmind')
142                     xmind.save(workbook, path=r'./' + sheet_name+ '.xmind')
143                     # 修复
144                     self.aftertreatment(r'C:./' + sheet_name + '.xmind')
145             return 'OK'
146         except:
147             pass
148 
149 
150 if __name__ == '__main__':
151     convert().gen_xmind_file(xls=r'./demo.xlsx')
main_fest.xml
 1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 2 <manifest xmlns="urn:xmind:xmap:xmlns:manifest:1.0">
 3     <file-entry full-path="comments.xml" media-type=""/>
 4     <file-entry full-path="content.xml" media-type="text/xml"/>
 5     <file-entry full-path="markers/" media-type=""/>
 6     <file-entry full-path="markers/markerSheet.xml" media-type=""/>
 7     <file-entry full-path="META-INF/" media-type=""/>
 8     <file-entry full-path="META-INF/manifest.xml" media-type="text/xml"/>
 9     <file-entry full-path="meta.xml" media-type="text/xml"/>
10     <file-entry full-path="styles.xml" media-type=""/>
11 </manifest>

 

posted @ 2023-03-29 21:15  H年轻的心  阅读(1527)  评论(3编辑  收藏  举报