1.下载xmind-SDK源码

1.1地址

https://github.com/xmindltd/xmind-sdk-python

1.2安装

执行安装:python3 setup.py instal

基于python实现xmind测试用例导出excel格式_实例化

 

 

# @Time    : 2021/7/21 12:21
# @Author : cici
"""
导入xmindparser模块是为了解析xmind获得字典列表
"""
import xmindparser
#导入xlwt模块是为了创建excel并写入数据的
import xlwt
#定义一个类
class Xmind2Excel():
#定义一个配置方法
def config(self):
# xmindparser导出格式配置
xmindparser.config = {
'showTopicId': False, # 是否展示主题ID
'hideEmptyValue': True, # 是否隐藏空值
'showRelationship': True # 新增配置,是否展示节点关系
}
#定义一个解析xmind内容的方法
def xmind(self):
filePath = '/Users/july/Desktop/客户端职能划分.xmind'
# 解析成dict数据类型
# 获取列表中的第1个字典中的topic:内容即包括一级标题在内的xmind内容,content行参
content = xmindparser.xmind_to_dict(filePath)[0]['topic']
#将content属性返回给方法的调用处self
return content
#创建一个excel,并输入表头,然后对表头字体进行设置
def styles(self,bold,height):
# 初始化样式,就是将excel样式初始化一个对象,可以理解为对这个样式类实例化对象style
style = xlwt.XFStyle()
# 为样式创建字体,也是对字体类实例化对象front
font = xlwt.Font()
font.name = 'Times New Roman'
# 加粗
font.bold = bold
# 字体大小,20是基数不变,18是字体大小
font.height = 20 * height
# 将设置的字体,赋值给类对象
style.font = font
#将配置好的格式返回给调用处
return style
#创建背景色
def patterns(self,colors=23):
"""
设置单元格的背景颜色,该数字表示的颜色在xlwt库的其他方法中也适用,默认颜色为白色
0 = Black, 1 = White,2 = Red, 3 = Green, 4 = Blue,5 = Yellow, 6 = Magenta, 7 = Cyan,
16 = Maroon, 17 = Dark Green,18 = Dark Blue, 19 = Dark Yellow ,almost brown), 20 = Dark Magenta,
21 = Teal, 22 = Light Gray,23 = Dark Gray, the list goes on...
"""
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = colors
return pattern
def buildExcel(self):
# 将styles方法中的格式赋值给新的对象,并将背景色方法赋值给给新的对象的背景色属性=style2.pattern
style2 = self.styles(True, 18)
style2.pattern = self.patterns()
# 创建一个excel
self.workexcel = xlwt.Workbook(encoding='utf-8')
# 创建一个worksheet对象就是excel中的第一个表,第二个参数是指单元格是否允许重设置,为了防止重复对一个单元格写入报错,设置允许true
self.worksheet = self.workexcel.add_sheet('test1', cell_overwrite_ok=True)
# 设置worksheet每一列的宽度15列,range是不包含15的
for m in range(15):
first_col = self.worksheet.col(m)
first_col.width = 256 * 20
# 表头
title = ['功能点', '前提条件', '执行步骤', '期望结果', '备注'] # 写成excel表格用例的要素
# 列表长度6
num = len(title)
# for循环遍历最大到5,从0开始不含6
for i in range(num):
# 往表格中填充表头
## 第一个参数代表行,第二个参数是列,第三个参数是内容,第四个参数是格式调用styles2格式
self.worksheet.write(0, i, title[i], style2)
def writeExcel(self):
# 调用类内方法时,如果调用的方法内参数写了self,那么调用方法时需要使用self.方法名()来调用
xm = self.xmind()
print(xm)
x = 1 # 初始化行数
# 第一级topics
for a in range(len(xm["topics"])): # topics列表中只有一个{}
case1 = xm["topics"][a] # 获取{}元祖给case1,元祖中有一级标题及下一级4个分支模块
print(x)
print(case1["title"]) # 第一级title
# 调用类内其他方法的属性,需要在方法中创建self.属性名,然后在需要调用的方法内部采用self.属性名调用
self.worksheet.write(x,0,case1["title"],self.styles(False,15))
# 第二级topics
if len(case1) >= 2:
for b in range(len(case1["topics"])): # 元祖中的四个模块
case2 = case1["topics"][b] # 将case1四个模块中的第一个模块的title和topics都给case2,case2中有2个功能点
print(x)
print(case2["title"]) # 第二级模块
self.worksheet.write(x, 1, case2["title"], self.styles(False, 15))
x += 1
# 第三级topics
if len(case2) >= 2:
x -= 1
for c in range(len(case2["topics"])): # 第三级功能点
case3 = case2["topics"][
c] # 将case2中的第一个功能点给case3,case3中有3个步骤,第二遍case3中只有功能点2,不满足len(case3)>=2
print(x)
print(case3["title"]) # 第三层功能点
self.worksheet.write(x, 2, case3["title"], self.styles(False, 15))
x += 1
# 第四级topics
if len(case3) >= 2:
x -= 1
for d in range(len(case3["topics"])):
case4 = case3["topics"][d] # 将case3中的第一个步骤给case4,case4中只有1个title
print(x)
print(case4["title"]) # 获取第4层内容
self.worksheet.write(x, 3, case4["title"], self.styles(False, 15))
x += 1
# 第五级topics
if len(case4) >= 2:
x -= 1
for e in range(len(case4["topics"])):
case5 = case4["topics"][e]
print(x)
print(case5["title"])
self.worksheet.write(x, 4, case5["title"], self.styles(False, 15))
x += 1
if len(case5) >= 2:
x -= 1
for h in range(len(case5["topics"])):
case6 = case5["topics"][h]
print(x)
print(case6["title"])
self.worksheet.write(x, 5, case6["title"], self.styles(False, 15))
# 保存workexcel文件名为testcase2,存在项目文件夹内
self.workexcel.save('testcase2.xls')
#self.worksheet.write_merge(1, 2, 0, 0, case1["title"])
#定义一个类对象,来调用类中的方法或者属性
xmindexcel=Xmind2Excel()
#调用方法体中的配置
xmindexcel.config()
xmindexcel.buildExcel()
xmindexcel.writeExcel()