鱼C论坛

 找回密码
 立即注册
查看: 1725|回复: 0

[作品展示] 对测试数据的自动整理出报告

[复制链接]
发表于 2020-4-4 12:04:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
最近老是整报告,搞了个半自动出报告的玩意。本来想用pack,还是搞得不清楚最后还是选择grid,相对简单点

  1. """
  2. 1.选择文件夹,获取文件夹中的txt文件
  3. 2.读取txt文件的内容(addr,addr2(hex),description,value)
  4. 3.上一步获取的txt文件内容的value值实际是十六进制
  5. 4.选择要使用的文件
  6. 5.选择报告模板(*.xlsx),并将第四步选择的文件的value值以0x00的格式保存到报告模板
  7. """
  8. import tkinter as tk
  9. from tkinter import filedialog
  10. import os
  11. import openpyxl

  12. transresult={}

  13. def filedirselect():
  14.     global filenamelist
  15.     global filedir
  16.     filedir = filedialog.askdirectory()  #选择数据位置
  17.    
  18.     filenamelist=[filename for filename in eo.listdir(filedir) if filename.endswith(".txt")]
  19.     #filenamelist= os.listdir(filedir)
  20.     print(filenamelist)
  21.    
  22.     if filedir != "":
  23.         entry1.delete(0,'end')
  24.     entry1.insert(0,filedir)
  25.    
  26.     pass

  27. def fileanalyse():  #分析测试数据
  28.     filenum=len(filenamelist)
  29.     if entry2.get() !="":
  30.         entry2.delete(0,'end')
  31.     entry2.insert(0,filenamelist)
  32.    
  33.     for i in range(filenum):
  34.         filename =filedir+'/'+filenamelist[i]
  35.         transresult[filenamelist[i]]=filetrans(filename)    #页名做键,页的registervalue列表做值 字典
  36.         
  37.     pass
  38. def filetrans(filename):    #对测试的txt数据进行转换
  39.     registervalue=[]
  40.     file=open(filename,'r')
  41.     for each_line in file:
  42.         each_linelist=each_line[:-1].split('\t')
  43.         registervalue.append('0x'+each_linelist[-1])
  44.     file.clegisee()
  45.     return registervalue
  46.     pass
  47. def modefileselect():
  48.     file=filedialog.askopenfilename()
  49.     if entry4.get() !='' and file !='':
  50.         entry4.delete(0,'end')
  51.     entry4.insert(0,file)
  52.     pass

  53. def writereport():
  54.     reportname=entry4.get()   #获取报告模板位置和名称
  55.     pagenamelist=entry3.get().split(',')    #获取需要处理的page列表
  56.     numofpage=len(pagenamelist)

  57.     wb=openpyxl.load_workbook(reportname)   #打开报告模板文件,为下一步的写入做准备
  58.     for each in pagelist:
  59.         wsheet=wb.get_sheet_by_name(each)   #打开要处理的page
  60.         i=2
  61.         while i<130:    #更新报告中对应sheet中C列的值
  62.             wsheet['C'+str(i)].value=transresullt[each+'.txt'][i-2]
  63.             i +=1
  64.     wb.save(reportname)   
  65.         
  66.     pass

  67. root  = tk.Tk(className='Code Validation trans')
  68. root.geometry("550x250+200+150")

  69. label1= tk.Label(root,text='数据位置',width=6)
  70. label1.grid(row=0,rowspan=1,column=1,padx=10,pady=15)
  71. #label1.pack(side='left',padx=10,pady=1)

  72. entry1=tk.Entry(root,width=36,font=6)
  73. entry1.grid(row=0,column=2,columnspan=3,padx=10,pady=15,sticky='n')
  74. #entry1.pack(side='left',padx=10,pady=1)

  75. button1=tk.Button(root,text='浏览...',command=filedirselect,width=6)
  76. button1.grid(row=0,column=5,columnspan=1,padx=10,pady=15)
  77. #button1.pack(side='left',padx=10,pady=1)

  78. button2=tk.Button(root,text='数据解析',command=fileanalyse,width=6)
  79. button2.grid(row=0,column=6,columnspan=1,padx=10,pady=15)
  80. #button2.pack(side='left',padx=10,pady=1)

  81. label2= tk.Label(root,text='text文件',width=6)
  82. label2.grid(row=1,rowspan=1,column=1,padx=10,pady=15)
  83. #label2.pack(side='top',padx=10,pady=1)

  84. entry2=tk.Entry(root,width=60)
  85. entry2.grid(row=1,column=2,columnspan=5,padx=10,pady=15,sticky='n')
  86. #entry2.pack(side='bottom',padx=10,pady=1)

  87. label3= tk.Label(root,text='选择text',width=6)
  88. label3.grid(row=2,rowspan=1,column=1,padx=10,pady=15)
  89. #label2.pack(side='top',padx=10,pady=1)

  90. entry3=tk.Entry(root,width=60)
  91. entry3.grid(row=2,column=2,columnspan=5,padx=10,pady=15,sticky='n')
  92. #entry2.pack(side='bottom',padx=10,pady=1)

  93. label2= tk.Label(root,text='选择模板',width=6)
  94. label2.grid(row=4,rowspan=1,column=1,padx=10,pady=15)
  95. #label1.pack(side='left',padx=10,pady=1)

  96. entry4=tk.Entry(root,width=36)
  97. entry4.grid(row=4,column=2,columnspan=3,padx=10,pady=15,sticky='n')
  98. #entry1.pack(side='left',padx=10,pady=1)

  99. button3=tk.Button(root,text='浏览...',command=modefileselect,width=6)
  100. button3.grid(row=4,column=5,columnspan=1,padx=10,pady=15)
  101. #button1.pack(side='left',padx=10,pady=1)

  102. button4=tk.Button(root,text='确定',command=writereport,width=6)
  103. button4.grid(row=4,column=6,columnspan=1,padx=10,pady=15)
  104. #button2.pack(side='left',padx=10,pady=1)
复制代码
pic1.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 19:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表