国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現將xml導入至excel

2020-01-04 17:56:18
字體:
來源:轉載
供稿:網友

本文給大家講解的是使用Python的Testlink實現將實現將xml導入至excel表格中,方法非常的簡單,另外附上其他小伙伴的方法,有需要的童鞋們可以參考下。

最近在使用Testlink時,發現導入的用例是xml格式,且沒有合適的工具轉成excel格式,xml使用excel打開顯示的東西也太多,網上也有相關工具轉成csv格式的,結果也不合人意。

那求人不如爾己,自己寫一個吧

需要用到的模塊有:xml.dom.minidom(python自帶)、xlwt

使用版本:

python:2.7.5

xlwt:1.0.0

一、先分析Testlink XML格式:

Python實現將xml導入至excel

這是一個有兩級testusuit的典型的testlink用例結構,我們只需要取testsuite name,testcase name,preconditions,actions,expectedresults

二、程序如下:
 

  1. #coding:utf-8 
  2. ''
  3. Created on 2015-8-20 
  4.  
  5. @author: Administrator 
  6. ''
  7. ''
  8. ''
  9. import xml.etree.cElementTree as ET 
  10. import xml.dom.minidom as xx 
  11. import os,xlwt,datetime 
  12.  
  13. workbook=xlwt.Workbook(encoding="utf-8"
  14.  
  15. booksheet=workbook.add_sheet(u'sheet_1'
  16. booksheet.col(0).width= 5120 
  17. booksheet.col(1).width= 5120 
  18. booksheet.col(2).width= 5120 
  19. booksheet.col(3).width= 5120 
  20. booksheet.col(4).width= 5120 
  21. booksheet.col(5).width= 5120 
  22.  
  23. dom=xx.parse(r'D://Python27/test.xml'
  24. root = dom.documentElement 
  25. row=1 
  26. col=1 
  27.  
  28. borders=xlwt.Borders() 
  29. borders.left=1 
  30. borders.right=1 
  31. borders.top=1 
  32. borders.bottom=1 
  33.  
  34.  
  35. style = xlwt.easyxf('align: wrap on,vert centre, horiz center') #自動換行、水平居中、垂直居中 
  36. #設置標題的格式,字體方宋、加粗、背景色:菊黃 
  37. #測試項的標題 
  38.  
  39. title=xlwt.easyxf(u'font:name 仿宋,height 240 ,colour_index black, bold on, italic off; align: wrap on, vert centre, horiz center;pattern: pattern solid, fore_colour light_orange;'
  40. item='測試項' 
  41. Subitem='測試分項' 
  42. CaseTitle='測試用例標題' 
  43. Condition='預置條件' 
  44. actions='操作步驟' 
  45. Result='預期結果' 
  46. booksheet.write(0,0,item,title) 
  47. booksheet.write(0,1,Subitem,title) 
  48. booksheet.write(0,2,CaseTitle,title) 
  49. booksheet.write(0,3,Condition,title) 
  50. booksheet.write(0,4,actions,title) 
  51. booksheet.write(0,5,Result,title) 
  52. #凍結首行 
  53. booksheet.panes_frozen=True 
  54. booksheet.horz_split_pos= 1 
  55.  
  56.  
  57. #一級目錄 
  58. for i in root.childNodes: 
  59. testsuite=i.getAttribute('name').strip() 
  60. #print testsuite 
  61. #print testsuite 
  62. ''
  63. 寫測試項 
  64. ''
  65. print "row is :",row 
  66. booksheet.write(row,col,testsuite,style) 
  67.  
  68.  
  69. #二級目錄 
  70. for dd in i.childNodes: 
  71. print " %s" % dd.getAttribute('name'
  72. testsuite2=dd.getAttribute('name'
  73. if not dd.getElementsByTagName('testcase'): 
  74. print "Testcase is %s" % testsuite2 
  75. row=row+1 
  76. booksheet.write(row,2,testsuite2,style) #寫測試分項 
  77.  
  78. row=row+1 
  79.  
  80. booksheet.write(row,1,testsuite2,style) 
  81. itemlist=dd.getElementsByTagName('testcase'
  82.  
  83. for subb in itemlist: 
  84. #print " %s" % subb.getAttribute('name') 
  85. testcase=subb.getAttribute('name'
  86.  
  87. row=row+1 
  88. booksheet.write(row,2,testcase,style) 
  89.  
  90. ilist=subb.getElementsByTagName('preconditions'
  91. for ii in ilist: 
  92. preconditions=ii.firstChild.data.replace("<br />"," "
  93. col=col+1 
  94. booksheet.write(row,3,preconditions,style) 
  95. steplist=subb.getElementsByTagName('actions'
  96. #print steplist 
  97. for step in steplist: 
  98. actions=step.firstChild.data.replace("<br />"," "
  99. col=col+1 
  100. booksheet.write(row,4,actions,style) 
  101. #print "測試步驟:",steplist[0].firstChild.data.replace("<br />"," ") 
  102. expectlist=subb.getElementsByTagName('expectedresults'
  103.  
  104. for expect in expectlist: 
  105. result=expect.childNodes[0].nodeValue.replace("<br />","" ) 
  106. booksheet.write(row,5,result,style) 
  107.  
  108.  
  109. row=row+1 
  110.  
  111. workbook.save('demo.xls'

寫入excel的效果如下:

Python實現將xml導入至excel

我們再來看個實例:

需要下載一個module:xlwt,如下是source code

 

 
  1. import xml.dom.minidom 
  2. import xlwt 
  3. import sys 
  4.  
  5. col = 0 
  6. row = 0 
  7.  
  8.  
  9. def handle_xml_report(xml_report, excel):  
  10. problems = xml_report.getElementsByTagName("problem"
  11. handle_problems(problems, excel) 
  12.  
  13.  
  14. def handle_problems(problems, excel): 
  15. for problem in problems: 
  16. handle_problem(problem, excel) 
  17.  
  18.  
  19. def handle_problem(problem, excel): 
  20. global row 
  21. global col 
  22. code = problem.getElementsByTagName("code")  
  23. file = problem.getElementsByTagName("file")  
  24. line = problem.getElementsByTagName("line")  
  25. message = problem.getElementsByTagName("message"
  26.  
  27. for node in code:  
  28. excel.write(row, col, node.firstChild.data) 
  29. col = col + 1 
  30. for node in file:  
  31. excel.write(row, col, node.firstChild.data)  
  32. col = col + 1 
  33. for node in line:  
  34. excel.write(row, col, node.firstChild.data)  
  35. col = col + 1 
  36. for node in message:  
  37. excel.write(row, col, node.firstChild.data)  
  38. col = col + 1 
  39. row = row+1 
  40. col = 0 
  41.  
  42. if __name__ == '__main__':  
  43. if(len(sys.argv) <= 1): 
  44. print ("usage: xml2xls src_file [dst_file]"
  45. exit(0) 
  46. #the 1st argument is XML report ; the 2nd is XLS report 
  47. if(len(sys.argv) == 2): 
  48. xls_report = sys.argv[1][:-3] + 'xls' 
  49. #if there are more than 2 arguments, only the 1st & 2nd make sense 
  50. else
  51. xls_report = sys.argv[2] 
  52. xmldoc = xml.dom.minidom.parse(sys.argv[1])  
  53. wb = xlwt.Workbook() 
  54. ws = wb.add_sheet('MOLint'
  55. ws.write(row, col, 'Error Code'
  56. col = col + 1 
  57. ws.write(row, col, 'file'
  58. col = col + 1 
  59. ws.write(row, col, 'line')  
  60. col = col + 1 
  61. ws.write(row, col, 'Description')  
  62. row = row + 1 
  63. col = 0 
  64. handle_xml_report(xmldoc, ws) 
  65. wb.save(xls_report) 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沭阳县| 夏津县| 玛曲县| 辽源市| 德钦县| 雅安市| 嘉荫县| 谷城县| 子长县| 安平县| 博乐市| 北京市| 阿巴嘎旗| 甘泉县| 浏阳市| 安吉县| 新绛县| 成武县| 灵宝市| 和平区| 平昌县| 惠来县| 怀来县| 云梦县| 衡阳市| 平武县| 涿州市| 乐昌市| 云南省| 多伦县| 太和县| 彩票| 井陉县| 曲靖市| 南城县| 尉犁县| 宁波市| 丰县| 涟水县| 宜昌市| 泰安市|