1. 程式人生 > >使用py,根據日誌記錄自動生成周報

使用py,根據日誌記錄自動生成周報

日誌格式如下,思路是如果檢測到檔案中的內容為5位或者8位,即12.11或18.12.11,同時存在.即認為當前行為日期資料
僅作為方便檢視日誌使用,生成指令碼時過濾此行。每次讀取到空白行的時候則認為下一條記錄為新一天的記錄,
另外如果讀取到====則認為本週的所有記錄已經結束,可生成並退出指令碼


12.21
剩餘壽命列表

12.20
監控換刀記錄表

===============================================
===============================================
指令碼

# -*- coding: utf-8 -*-
"""
12.22 2:12AM """ #import time import datetime import xlwt now=datetime.datetime.now() workbook=xlwt.Workbook(encoding='utf-8') worksheet = workbook.add_sheet('Sheet1') count=1#序號 newDay=1#是否為另一天的記錄 f=open(r"C:\Users\Dell\Desktop\a.txt") i=4#行號 hour=6#工時 context=0 ids=1 thing=2#工作事項 project=3#所屬專案 skill=4#
技術點分解描述 other=5#需要協調內容(多人介面聯調、現場測試、客戶試用等等) planStart=7#計劃開始時間 planEnd=8#計劃結束時間 start=9#時間開始時間 end=10#時間完成時間 statu=11#完成狀態 backup=12#總結備案 #樣式 borders = xlwt.Borders() # Create borders borders.left = xlwt.Borders.THIN # 新增邊框-虛線邊框 borders.right = xlwt.Borders.THIN # 新增邊框-虛線邊框 borders.top = xlwt.Borders.THIN #
新增邊框-虛線邊框 borders.bottom = xlwt.Borders.THIN # 新增邊框-虛線邊框 borders.left_colour = 0x08 # 邊框上色 borders.right_colour = 0x08 borders.top_colour = 0x08 borders.bottom_colour = 0x08 #列寬 worksheet.col(context).width=256*10 worksheet.col(ids).width=256*8 worksheet.col(thing).width=256*30 worksheet.col(project).width=256*10 worksheet.col(skill).width=256*20 worksheet.col(other).width=256*25 worksheet.col(hour).width=256*10 worksheet.col(planStart).width=256*25 worksheet.col(planEnd).width=256*25 worksheet.col(start).width=256*25 worksheet.col(end).width=256*25 worksheet.col(statu).width=256*12 worksheet.col(backup).width=256*22 #建立一個樣式---------------------------- stylefirstRow = xlwt.XFStyle() stylefirstRow.borders = borders pattern = xlwt.Pattern() pattern.pattern =xlwt. Pattern.SOLID_PATTERN pattern.pattern_fore_colour = xlwt.Style.colour_map['gold'] #設定單元格背景色為黃色 stylefirstRow.pattern = pattern al = xlwt.Alignment() al.horz = 0x02 # 設定水平居中 al.vert = 0x01 # 設定垂直居中 al.wrap=1 #自動換行 stylefirstRow.alignment = al font = xlwt.Font() # 為樣式建立字型 #font.name = 'Times New Roman' font.name = u'微軟雅黑' #font.bold = True # 黑體 #font.underline = True # 下劃線 #font.italic = True # 斜體字 font.height=0x00C8*1.6 # C8 in Hex (in decimal) = 10 points in height. #font.colour_index=48#藍色 #font.colour_index=2#紅色 #font.colour_index=59#灰色 #font.colour_index=44#淺藍色 stylefirstRow.font = font # 設定樣式 #第二行樣式---------------------------- stylesecondRow = xlwt.XFStyle() stylesecondRow.borders = borders font2 = xlwt.Font() # 為樣式建立字型 font2.name = u'微軟雅黑' font2.height=0x00C8*1.4 # C8 in Hex (in decimal) = 10 points in height. stylesecondRow.font = font2 # 設定樣式 stylesecondRow.alignment = al #第3行樣式---------------------------- style3Row = xlwt.XFStyle() style3Row.borders = borders font3 = xlwt.Font() # 為樣式建立字型 font3.name = u'微軟雅黑' font3.height=0x00C8 # C8 in Hex (in decimal) = 10 points in height. font3.colour_index=48#藍色 style3Row.font = font3 # 設定樣式 #style3Row.alignment = al #統一字型 fontx = xlwt.Font() # 為樣式建立字型 fontx.name = u'微軟雅黑' fontx.height=0x00C8*1.2 # C8 in Hex (in decimal) = 10 points in height. #灰色背景 styleGrayRow = xlwt.XFStyle() styleGrayRow.borders = borders styleGrayRow.font=fontx patternGray = xlwt.Pattern() patternGray.pattern =xlwt. Pattern.SOLID_PATTERN patternGray.pattern_fore_colour = xlwt.Style.colour_map['gray25'] styleGrayRow.pattern=patternGray styleGrayRow.alignment = al #淺綠背景 styleLightGreenRow = xlwt.XFStyle() styleLightGreenRow.borders = borders styleLightGreenRow.font=fontx patternLightGreen = xlwt.Pattern() patternLightGreen.pattern =xlwt. Pattern.SOLID_PATTERN patternLightGreen.pattern_fore_colour = xlwt.Style.colour_map['light_green'] styleLightGreenRow.pattern=patternLightGreen styleLightGreenRow.alignment=al styleLightGreenRow.alignment = al #綠色背景 styleGreenRow = xlwt.XFStyle() styleGreenRow.borders = borders styleGreenRow.font=fontx patternGreen = xlwt.Pattern() patternGreen.pattern =xlwt. Pattern.SOLID_PATTERN patternGreen.pattern_fore_colour = xlwt.Style.colour_map['green'] styleGreenRow.pattern=patternGreen #設定單元格背景色 styleGreenRow.pattern = patternGreen styleGreenRow.alignment = al #紅色字 redstyle = xlwt.XFStyle() redstyle.borders = borders fontred= xlwt.Font() # 為樣式建立字型 fontred.name = u'微軟雅黑' fontred.height=0x00C8*1.2 # C8 in Hex (in decimal) = 10 points in height. fontred.colour_index=2#紅色 redstyle.font=fontred redstyle.pattern=patternGray redstyle.alignment = al #通用style styleCommon = xlwt.XFStyle() styleCommon.borders = borders styleCommon.font=fontx styleCommon.alignment = al #模板內容 worksheet.write_merge(0, 0,0,12, label = "產品開發部工作週報",style=stylefirstRow) worksheet.write_merge(1, 1,5,7, label = "張三",style=stylesecondRow) worksheet.write_merge(2, 2,0,12, label = "注:每週五時做本週工作總結(關表),同時做下週工作計劃(開表),必要時下週一調整;",style=style3Row) worksheet.write(3, context, label = "內容",style=styleGrayRow) worksheet.write(3, ids, label = "序號",style=styleGrayRow) worksheet.write(3, thing, label = "工作事項",style=styleGrayRow) worksheet.write(3, project, label = "所屬專案",style=styleGrayRow) worksheet.write(3, skill, label = "技術點分解描述",style=styleGrayRow) worksheet.write(3, other, label = "需要協調內容(多人介面聯調、現場測試、客戶試用等等)",style=redstyle) worksheet.write(3, hour, label = "工作估時",style=styleGrayRow) worksheet.write(3, planStart, label = "計劃開始時間",style=styleGrayRow) worksheet.write(3, planEnd, label = "計劃結束時間",style=styleGrayRow) worksheet.write(3, start, label = "實際開始時間",style=styleGreenRow) worksheet.write(3, end, label = "實際開始時間",style=styleGreenRow) worksheet.write(3, statu, label = "完成狀態",style=styleGreenRow) worksheet.write(3, backup, label = "總結備案",style=styleGreenRow ) for line in f: line=line.replace('\n','') #如果上一次獲取到的行為空格,則認為當前行為日期,直接跳過 if newDay==1: newDay=0 #搜尋到##則認為一週結束 if line.find("====")!=-1: break else: #print strTime #如果獲取的內容為空格則開始新一天的資料 if line=="": #更新當前的日期 newDay=1 now=now+datetime.timedelta(days=-1) else: newDay=0#置為非新一天的資料 # print len(line)#==8 or line.count==5) if (len(line)==8 or len(line)==5) and line.find(".")!=-1: continue#print line.find(".") else: worksheet.write(i, ids, label = count,style=styleCommon) worksheet.write(i, thing, label = line,style=styleCommon) worksheet.write(i, project, label = "",style=styleCommon) worksheet.write(i, skill, label = "",style=styleCommon) worksheet.write(i, other, label = "",style=styleCommon) worksheet.write(i, hour, label = "9",style=styleCommon) worksheet.write(i, planStart, label = now.strftime("%Y-%m-%d"),style=styleCommon) worksheet.write(i, planEnd, label = now.strftime("%Y-%m-%d"),style=styleCommon) worksheet.write(i, start, label = now.strftime("%Y-%m-%d"),style=styleCommon) worksheet.write(i, end, label = now.strftime("%Y-%m-%d"),style=styleCommon) worksheet.write(i, statu, label = "完成",style=styleCommon) worksheet.write(i, backup, label = "",style=styleCommon) print now.strftime("%Y-%m-%d") +"***"+line.decode('utf-8').encode('cp936') i=i+1 count=count+1 #print strTime worksheet.write_merge(4, count+4-2,0,0, label = "本週工作總結",style=styleLightGreenRow) f.close() workbook.save(r"C:\Users\Dell\Desktop\\"+"產品開發週報_張三_".decode('utf-8').encode('cp936')+datetime.datetime.now().strftime("%Y_%m_%d")+".xlsx")

 程式碼:github