1 #!/usr/bin/env python3 2 #-*- coding:utf-8 -*- 3 ''' 4 Administrator 5 2018/8/17 6 ''' 7 # import xlrd 8 # 9 # data=xlrd.open_workbook("单县.xls") 10 # 11 # table=data.sheets()[0] 12 # table = data.sheet_by_index(0) #通过索引顺序获取 13 # table = data.sheet_by_name(u'Sheet1')#通过名称获取 14 # 4、获取整行和整列的值(返回数组) 15 # table.row_values(i) 16 # table.col_values(i) 17 # 5、获取行数和列数 18 # table.nrows 19 # table.ncols 20 # 6、获取单元格 21 # table.cell(0,0).value 22 # table.cell(2,3).value 23 # print(table.row_values(3)) 24 # print(table.row_values(16)) 25 # print(table.col_values(0)) 26 27 # a=table.cell(16,14).value 28 # a=table.cell(16,14).value 29 # print(a,type(a)) #2.0 <class 'float'> 30 from xlrd import open_workbook 31 from xlutils.copy import copy 32 33 data=open_workbook("new1.xlsx") 34 table=data.sheet_by_index(0) #得到sheet1 对象 35 print(table.ncols)#3列 36 print(table.nrows)#10行 37 # for i in range(table.nrows): 38 # # print(i) 39 # for j in range(table.nrows): 40 # print(table.cell(j,i).value,end=" ") 41 # print() 42 for i in range(table.nrows):#10行 43 for j in range(table.ncols):#3列 44 item=table.cell(i, j).value 45 if type(item) is str: 46 print("这是一个字符串%s"%item) 47 else: 48 print(item,type(item),end=" ") 49 print() 50 51 52 # wb=copy(rb)# 利用xlutils.copy函数,将xlrd.Book转为xlwt.Workbook,再用xlwt模块进行存储 53 # #通过get_sheet()获取的sheet有write()方法 54 # ws = wb.get_sheet(0) 55 # # ws.write(0, 0, 'changed!') 56 # # wb.save('new2.xlsx') 57 # print(ws) #<xlwt.Worksheet.Worksheet object at 0x000000000A83FB00> 58 # row=ws.nrows 59 # # col=ws.ncols 60 # print(row)