在测试过程中,我们写的用例要分享给别人,用html格式的很不好看。又没有更好的软件来一键转换,没办法,只有自己写脚本
用到:
BeautifulSoup
xlwt
代码如下:
from bs4 import BeautifulSoup import xlwt workbook = xlwt.Workbook(encoding='utf-8') worksheet = workbook.add_sheet("My sheet") f = open(r"F:xxx.html") ff = f.read() soup = BeautifulSoup(ff, 'lxml') bb = soup.find_all(attrs={'class', 'testcase'}) for i, b in enumerate(bb): print b['id'] worksheet.write(i+1, 0, label = b['id']) sss = b.td.get_text().split() if len(sss) >= 2: print sss[0], sss[1] else: print sss[0], u"无描述" worksheet.write(i + 1, 1, label=sss[0]) if len(sss) >= 2: worksheet.write(i + 1, 2, label=sss[1]) else: worksheet.write(i + 1, 2, label=u'无描述') workbook.save('Excel_test.xls')