首页 > 编程语言 > 详细

帮同事的一个python处理excel的过程

时间:2019-03-15 17:51:49      阅读:160      评论:0      收藏:0      [点我收藏+]

帮同事的一个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)

第一

 

帮同事的一个python处理excel的过程

原文:https://www.cnblogs.com/wuhaijian/p/10538498.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!