101 lines
3.3 KiB
Python
101 lines
3.3 KiB
Python
# -*- coding: utf-8 -*-
|
||
from ExcelToXml import ExcelToXml
|
||
import os
|
||
import sys
|
||
|
||
|
||
# 定义后缀过滤列表
|
||
filter_list = ['.xls']
|
||
|
||
# 说明:检查output文件夹是否存在,不存在则创建
|
||
# 参数:无
|
||
# 返回值:输出路径
|
||
def get_output_dir():
|
||
output = os.path.join(os.getcwd(), 'output')
|
||
if not os.path.exists(output):
|
||
os.mkdir(output)
|
||
return output
|
||
|
||
|
||
# 说明:遍历指定文件夹扫描Excel文件
|
||
# 参数:无
|
||
# 返回值:输出文件夹下所有文件路径列表
|
||
def get_input_excel(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():
|
||
excels = []
|
||
input_dir = ''
|
||
# 未传入参数,使用默认设置
|
||
if len(sys.argv) == 1:
|
||
input_dir = os.path.join(os.getcwd(), 'input')
|
||
excels = get_input_excel(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]
|
||
# 判断是否是Excel
|
||
if ext not in filter_list:
|
||
print(u'ERROR:请传入正确的Excel文件')
|
||
return False
|
||
excels.append(input_dir)
|
||
elif os.path.exists(input_dir): # 如果是文件夹
|
||
excels = get_input_excel(input_dir)
|
||
return excels, input_dir
|
||
|
||
|
||
# 说明:将指定Excel转换为TestLink使用的xml文件
|
||
# 参数:无
|
||
# 返回值:成功返回True
|
||
def converter():
|
||
# 获取命令行参数
|
||
excels, input_dir = get_command()
|
||
# 校验文件输入
|
||
if len(excels) == 0:
|
||
print(u'ERROR: 未发现输入的Excel文件')
|
||
return False
|
||
print('--------------------------------')
|
||
print u'INFO: 当前输入文件为:%s' % input_dir.decode('gb2312')
|
||
output = get_output_dir()
|
||
# 遍历文件输出
|
||
print(u'INFO: 开始执行Excel转xml...')
|
||
print('--------------------------------')
|
||
for excel in excels:
|
||
# 文件名
|
||
_, filename = os.path.split(excel)
|
||
# 实例化对象
|
||
xm = ExcelToXml(excel)
|
||
if not xm.create_tree():
|
||
print 'ERROR: [%s]转换发生错误' % filename.decode('gb2312')
|
||
print('------------------------')
|
||
continue
|
||
# 写入xml文件
|
||
xm.write_xml(output)
|
||
result = xm.get_result()
|
||
print u'[%s]转换成功!' % filename.decode('gb2312')
|
||
print u'Sheet总数为:\t%s' % result[0]
|
||
print u'模块总数为:\t%s' % result[1]
|
||
print u'案例总数为:\t%s' % result[2]
|
||
print('------------------------')
|
||
print u'INFO: 全部转换成功,已输出到文件夹:%s' % output.decode('gb2312')
|
||
return True
|
||
|
||
|
||
if __name__ == "__main__":
|
||
# 开始转换
|
||
converter() |