帮同事的一个python处理excel的过程
只罗列的一下,没有方法和类
1 import os 2 import sys 3 import datetime 4 import xlrd 5 import xlwt 6 from xlutils.copy import copy 7 import re 8 9 dt = (datetime.datetime.now()-datetime.timedelta(days=0)).strftime("%Y-%m-%d") # print(dt) 10 filelist = [] 11 fname = "" 12 #pth = os.listdir(os.getcwd()) # print(os.listdir(os.chdir("e:/python/py001"))) print(pth) 13 pth = os.listdir(sys.path[0]) 14 15 def checkfile(datedt,pathpth): 16 for s in pathpth: 17 if datedt in s: 18 filelist.append(s,) 19 20 for i in filelist: 21 if "ZH" in i: 22 # fname = i 23 # print(fname) 24 return i 25 26 fname = checkfile(dt,pth) 27 #print(fname) 28 29 30 li = [] 31 #report = "e:/python/py001/vm_2019-03-06_18-12-096013759B0DF190B8C0129B412E88C32E_ZH.xls" 32 #report = fname 33 fname = ("%s\%s" % (sys.path[0],fname)) 34 #print(fname) 35 wk = xlrd.open_workbook(fname) 36 sheet = wk.sheet_by_index(0) 37 rows = sheet.nrows 38 cols = sheet.ncols 39 #li.append(sheet.cell(1,6).value) 40 for i in range(rows): 41 #if i==0: 42 #continue 43 li.append(sheet.cell(i,6).value,) 44 45 #li.remove("主机") 46 #print(len(li)) 47 48 49 a=open("%s\zhuji.txt"% (sys.path[0])) 50 dic={} 51 for line in a: 52 line = re.sub('"','',line) 53 line = re.sub('\n','',line) 54 line = re.sub(',',':',line) 55 li.append(line,) 56 dic[line.split(":")[0]]=line.split(":")[1] 57 58 wd = copy(wk) 59 sheetwt = wd.get_sheet(0) 60 61 for i in range(rows): 62 if i==0: 63 sheetwt.write(i, 9, "主机名") 64 continue 65 #print(li[i]) 66 sheetwt.write(i,9,dic.get(li[i],'新主机')) 67 68 os.remove(fname) 69 wd.save(fname)
第一