98 lines
3.1 KiB
Python
98 lines
3.1 KiB
Python
# -*- coding: utf-8 -*-
|
||
from XmlToExcel import XmlToExcel
|
||
import sys
|
||
import os
|
||
import time
|
||
|
||
# 定义后缀过滤列表
|
||
filter_list = ['.xml']
|
||
|
||
# 说明:检查output文件夹是否存在,不存在则创建
|
||
# 参数:无
|
||
# 返回值:输出路径
|
||
def get_output_dir():
|
||
output = os.path.join(os.getcwd(), 'output')
|
||
if not os.path.exists(output):
|
||
os.mkdir(output)
|
||
return output
|
||
|
||
|
||
# 说明:遍历指定文件夹扫描xml文件
|
||
# 参数:无
|
||
# 返回值:输出文件夹下所有xml文件路径列表
|
||
def get_input_xml(input_dir):
|
||
files = []
|
||
# input文件夹不存在
|
||
if not os.path.exists(input_dir):
|
||
return files
|
||
else: # 遍历该目录下所有文件
|
||
for _, _, file_list in os.walk(input_dir):
|
||
for filename in file_list:
|
||
file_path = os.path.join(input_dir, filename)
|
||
# 获取文件后缀
|
||
ext = os.path.splitext(file_path)[1]
|
||
# 满足要求的则放入
|
||
if ext in filter_list:
|
||
files.append(file_path)
|
||
return files
|
||
|
||
# 将命令行参数转换为对应的文件输入
|
||
def get_command():
|
||
xmls = []
|
||
input_dir = ''
|
||
# 未传入参数,使用默认设置
|
||
if len(sys.argv) == 1:
|
||
input_dir = os.path.join(os.getcwd(), 'input')
|
||
xmls = get_input_xml(input_dir)
|
||
elif len(sys.argv) == 2: # 传入了参数
|
||
input_dir = sys.argv[1]
|
||
# 如果传入的是单个文件
|
||
if os.path.isfile(input_dir):
|
||
ext = os.path.splitext(input_dir)[1]
|
||
# 判断是否是xml
|
||
if ext not in filter_list:
|
||
print(u'ERROR:请传入正确的xml文件')
|
||
return False
|
||
xmls.append(input_dir)
|
||
elif os.path.exists(input_dir): # 如果是文件夹
|
||
xmls = get_input_xml(input_dir)
|
||
return xmls, input_dir
|
||
|
||
|
||
# 说明:将指定xml转换为Excel文件
|
||
# 参数:无
|
||
# 返回值:成功返回True
|
||
def converter():
|
||
# 获取命令行参数
|
||
xmls, input_dir = get_command()
|
||
# 校验文件输入
|
||
if len(xmls) == 0:
|
||
print('ERROR: 未发现输入的xml文件')
|
||
return False
|
||
print('--------------------------------')
|
||
print('INFO: 当前输入文件为:', input_dir)
|
||
output = get_output_dir()
|
||
# 遍历文件输出
|
||
print('INFO: 开始执行xml转Excel...')
|
||
print('--------------------------------')
|
||
# 实例化
|
||
xm = XmlToExcel()
|
||
# 返回转换结果
|
||
result = xm.read_xml(xmls)
|
||
file_name = time.strftime(r'%Y%m%d%H%M%S', time.localtime()) + '_Example_Testsuite_Default.xls'
|
||
excel = os.path.join(output, file_name)
|
||
# 写入Excel文件
|
||
res = xm.write_excel(excel)
|
||
|
||
print('成功转换文件数:\t', result['success'])
|
||
print('失败转换文件数:\t', result['fail'])
|
||
print('模块总数为:\t', res['module'])
|
||
print('案例总数为:\t', res['case'])
|
||
print('------------------------')
|
||
print('INFO: 全部转换成功,已输出到文件:', excel)
|
||
return True
|
||
|
||
|
||
if __name__ == "__main__":
|
||
# 开始转换
|
||
converter() |