使用xlrd模块

一、安装xlrd模块

  到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境。

 二、Excel文件读取

# 导入包
import xlrd

# 打开excel文件读取数据
data = xlrd.open_workbook(filename)
data = xlrd.open_workbook('excelFile.xls') # 文件路径

三、单元格中的数据类型

0 empty,
1 string(text), 
2 number, 
3 date, 
4 boolean, 
5 error, 
6 blank
    

四、常用的函数

        excel中最重要的方法就是book和sheet的操作。

        1)获取book中一个工作表

  table = data.sheets()[0]          #通过索引顺序获取
  table = data.sheet_by_index(sheet_indx))    #通过索引顺序获取,0,1,2..
  table = data.sheet_by_name(sheet_name)      #通过名称获取

        以上三个函数都会返回一个xlrd.sheet.Sheet()对象

   names = data.sheet_names()    #返回book中所有工作表的名字
   data.sheet_loaded(sheet_name or indx)   # 检查某个sheet是否导入完毕

        2)行(row)的操作

    nrows = table.nrows  #获取该sheet中的有效行数

    table.row(rowx)  #返回由该行中所有的单元格对象组成的列表
cell_A1 = table.row(0)[0].value #行索引
table.row_slice(rowx) #返回由该列中所有的单元格对象组成的列表 table.row_types(rowx, start_colx=0, end_colx=None) #返回由该行中所有单元格的数据类型组成的列表 table.row_values(rowx, start_colx=0, end_colx=None) #返回由该行中所有单元格的数据组成的列表 table.row_len(rowx) #返回该列的有效单元格长度

        3)列(colnum)的操作

    ncols = table.ncols   #获取列表的有效列数

    table.col(colx, start_rowx=0, end_rowx=None)  #返回由该列中所有的单元格对象组成的列表
cell_A2 = table.col(1)[0].value #列索引
table.col_slice(colx, start_rowx=0, end_rowx=None) #返回由该列中所有的单元格对象组成的列表 table.col_types(colx, start_rowx=0, end_rowx=None) #返回由该列中所有单元格的数据类型组成的列表 table.col_values(colx, start_rowx=0, end_rowx=None) #返回由该列中所有单元格的数据组成的列表

         4)单元格的操作

    table.cell(rowx,colx)   #返回单元格对象

    table.cell_type(rowx,colx)    #返回单元格中的数据类型

    table.cell_value(rowx,colx)   #返回单元格中的数据

    table.cell_xf_index(rowx, colx)   # 暂时还没有搞懂

      cell_A1 = table.cell(0,0).value   # 返回单元格的值
      cell_C4 = table.cell(2,3).value
 

 五、读取Excel表格中的日期格式

在使用xlrd读取Excel表格中的日期格式时,读出的内容和原来Excel表格中的内容不一致。读取出来的是一个浮点数。导致不能正确使用。而xldate_as_tuple方法可以很好地解决这个问题。
函数定义:
def xldate_as_tuple(xldate, datemode)
参数一:要处理的单元格值
参数二:时间基准(0代表以1900-01-01为基准,1代表以1904-01-01为基准)
返回值:返回一个元组,他的值类似于(year, month, day, hour, minute, nearest_second)

  Demo:

# 将单元格的值(date类型),转换成字符串日期格式
def value_to_date(rawdate):
    """
    function: Converts a floating point number to a date
    :param rawdate: rawdate, for example: 43889.375625
    :return: date_tmp, for example:'2020-2-28 9:0:54'
    """
    date_value = xldate_as_tuple(rawdate, 0)
    date_value_str = [str(i) for i in date_value]
    date_tmp = '-'.join(date_value_str[0:3]) + ' ' + ':'.join(date_value_str[3:])
    # print(date_tmp)
    return date_tmp


# 读写excel文件,写入oracle数据库
def read_excel_eFPAData(db, file_path, file_prefix):    #file_path是文件路径,file_prefix是文件名前缀(前缀相当于:abc.xlsx不要.xlsx)
    print(file_path)
    wb = xlrd.open_workbook(filename=file_path)
    print(wb.sheet_names, 'sheet_name')

    sheet1 = wb.sheet_by_index(0)
    print(sheet1)
    print(sheet1.name, sheet1.nrows, sheet1.ncols)
    for i in range(1, sheet1.nrows):
        sheet_dict = {}
        rows = sheet1.row_values(i)

        sheet_dict['ESN'] = int(rows[0])
        sheet_dict['FuelVolumeTotal'] = rows[1]
        sheet_dict['DEFVolumeTotal'] = rows[2]
        row3 =rows[3] if (sheet1.row_types(i, start_colx=0, end_colx=None)[-1]) == 1 else value_to_date(rows[3])
        sheet_dict['OccurrenceTime'] = row3
        print(sheet_dict)

        sqlstr = """
                insert into flxuser2.BASE_00059_3(ESN, FuelVolumeTotal, DEFVolumeTotal, OccurrenceTime)
                values (:ESN, :FuelVolumeTotal, :DEFVolumeTotal, to_date(:OccurrenceTime, 'YYYY-MM-DD HH24:MI:SS'))
                """
        parameters = {'ESN': sheet_dict['ESN'],
                      'FuelVolumeTotal': sheet_dict['FuelVolumeTotal'],
                      'DEFVolumeTotal': sheet_dict['DEFVolumeTotal'],
                      'OccurrenceTime': sheet_dict['OccurrenceTime']
                      }

        try:
            db.insert(sqlstr, parameters)
            logger.writeLog('数据写入数据库->' + json.dumps(parameters), file_prefix[0] + ".log")
        except:
            pass

 

posted @ 2020-03-17 16:40  Amorphous  阅读(462)  评论(0编辑  收藏  举报