• python操作excel


     1 # !/usr/bin/python
     2 # -*- coding:UTF-8 -*-
     3 # author:jiyanjiao
     4 # 基础包:excel的封装
     5 
     6 import xlrd
     7 
     8 class readExcel(object):
     9     def __init__(self,path):
    10         self.path = path
    11 
    12     @property
    13     def getSheet(self):
    14         # 获取索引
    15         xl = xlrd.open_workbook(self.path)
    16         #sheet = xl.sheet_by_index(0)
    17         table = xl.sheets()[0]
    18         #print('table的返回值为:',table)
    19         return table
    20 
    21     @property
    22     def getRows(self):
    23         # 获取行数
    24         nrows = self.getSheet.nrows
    25         print('getrows的值:',nrows)
    26         return nrows
    27 
    28     @property
    29     def getCol(self):
    30         # 获取列数
    31         col = self.getSheet.ncols
    32         return col
    33 
    34     """以下是分别获取每一列的数值"""
    35 
    36     @property
    37     def getName(self):
    38         test_name = []
    39         for i in range(1,self.getRows):
    40             test_name.append(self.getSheet.cell_value(i,0))
    41         return test_name
    42 
    43     @property
    44     def getData(self):
    45         test_data = []
    46         for i in range(1,self.getRows):
    47             test_data.append(self.getSheet.cell_value(i,1))
    48         return test_data
    49 
    50 if __name__ == '__main__':
    51     ex_path = 'C:\Users\lenovo\Desktop\jiekou.xlsx'
    52     re = readExcel(ex_path)
    53     rows = re.getRows
    54     print('行数为:',rows)
    55     names = re.getName
    56     print('姓名:',names)
  • 相关阅读:
    bat 笔记 一
    air 桌面应用发布后可以删除的文件
    as3 去掉字符串空白问题
    as3 air 获取文件夹下的所有文件
    egret 配置设置
    egret 精简游戏项目
    starling 第一天
    《笨办法学Python》 第2课手记
    《笨办法学Python》 第1课手记
    《笨办法学Python》 第0课手记
  • 原文地址:https://www.cnblogs.com/jiyanjiao-702521/p/10063124.html
Copyright © 2020-2023  润新知