Python利用xlrd模板读取excel
1 #!/usr/bin/env python 2 #coding:utf-8 3 import xlrd,os,datetime 4 5 data=xlrd.open_workbook(u"\\\\10.25.1.9\\Dpt_IT\\基础架构部\\服务器_系统\\3.配置信息\\IDC设备配置信息列表.xlsx") #打开excel文件,Windows下 6 table=data.sheet_by_index(0) #通过索引选择第一个sheet 7 nrows=table.nrows #行数 8 ncols=table.ncols #列数 9 10 item=["Item","HostName","IP","外部IP","域名","环境","描述","类型","基础运维负责人","应用管理员","位置","设备类型","硬件厂商","型号","SN","CPU","DISK","MEM","OS","APP","开放端口","备注","日期"] 11 key=[chr(i) for i in range(65,88)] #生产列名A,B,C,D,E...W 12 choice=dict(zip(key,item)) 13 ncols_dir=dict(zip(key,range(len(choice)))) 14 data_list=[] 15 for i in range(5,nrows): 16 data_list.append(table.row_values(i)) #生成每行数据组成的列表 17 18 def show(chosen): 19 chosen=chosen.split(',') 20 chosen=[i.upper() for i in chosen] 21 print "*"*len(chosen)*50 22 for i in range(4,nrows): 23 for j in chosen: 24 ctype=table.cell(i,ncols_dir[j]).ctype 25 if ctype == 3: 26 date_value=xlrd.xldate_as_tuple(table.cell(i,ncols_dir[j]).value,data.datemode) 27 date_value=datetime.date(*date_value[:3]).strftime("%Y/%m/%d") 28 print "%-50s" % date_value, 29 elif ctype == 0: 30 print "%-50s" % "NULL", 31 else: 32 print "%-50s" % table.cell(i,ncols_dir[j]).value, #打印每行指定列的元素 33 print 34 35 while True: 36 print ''' 37 ****************************************** 38 1.query 39 2.see all 40 3.exit 41 ''' 42 print 43 chosen_num=raw_input("Pls enter ur choice:") 44 #类型: 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error 45 if chosen_num == '1': 46 for i,j in sorted(choice.items()): 47 print i+"--->"+j 48 chosen=raw_input("Pls enter ur choice(以,分隔,不区分大小写):") 49 show(chosen) 50 elif chosen_num == '2': 51 chosen=','.join(key) 52 show(chosen) 53 elif chosen_num == '3': 54 exit(0) 55 elif chosen_num == '': 56 pass 57 else: 58 pass