zoukankan      html  css  js  c++  java
  • python读取EXCEL文件

    # -*- coding:utf-8 -*-
    # Author : liuqingzheng
    # Data : 2018/11/27 17:26
    # 导入模块
    import xlrd
    from datetime import datetime,date
    # 打开要读的excel
    tt=xlrd.open_workbook('tt.xlsx')
    # 打印所有表格名字
    print(tt.sheet_names())
    #通过索引获取表格
    sheet1 = tt.sheet_by_index(0)
    print(sheet1)
    #通过名字获取表格
    # sheet2 = tt.sheet_by_name('个人信息')
    # sheet1.nrows 该表格行数
    # sheet1.ncols 该表格列数
    print(sheet1.name,sheet1.nrows,sheet1.ncols)
    # 根据行数,获取该行所有内容,放到列表中
    rows = sheet1.row_values(2)
    print(rows)
    # 根据列数,获取该列所有内容,放到列表中
    cols = sheet1.col_values(3)#获取列内容
    print(cols)
    # 获取表格里的内容,三种方式(获取第一行第二列,从0开始计算)
    print(sheet1.cell(1, 2).value)
    print(sheet1.cell_value(1, 2))
    print(sheet1.row(1)[2].value)
    # 通过列取
    print(sheet1.col(2)[1].value)
    
    
    # print(sheet1.row(4))
    
    print(sheet1.cell(1,3).ctype)
    # print(tt.datemode)
    '''
    默认情况下,Excel for Windows使用1900日期系统,而Excel for Macintosh使用1904日期系统。
    
    找了一通资料才知道原來是为了要处理闰年问题… XD (註) 另,原來 Excel (on Windows) 內部资料也是用 1900-based 的日期 (这是为了和旧式的 Lotus 1-2-3 相容),而 Excel (on Mac) 是 1904-based
    '''
    #**********处理日期格式
    date_value = xlrd.xldate_as_tuple(sheet1.cell_value(1,3),tt.datemode)
    # 打印出数组形式的日期
    print(date_value)
    # 用data模块处理一下时间
    # print(date(*date_value[0:3]))
    print(date(*date_value[:3]))
    print(date(*date_value[:3]).strftime('%Y-%m-%d'))
    # *********merged_cells
    # (1, 3, 4, 5)的含义是:第1到2行(不包括3)合并,(7, 8, 2, 5)的含义是:第2到4列合并
    # [(4, 5, 2, 6)]:表示第2列到第5列合并
    print(sheet1.merged_cells)
    print(sheet1.row_values(4,2))
    # 所以要取值,要取第4行,第2列,也就是低位索引
    print(sheet1.cell_value(4,2))
    
    
    merge = []
    print(sheet1.merged_cells)
    for (rlow,rhigh,clow,chigh) in sheet1.merged_cells:
        merge.append([rlow,clow])
    for index in merge:
        print(sheet1.cell_value(index[0],index[1]))
  • 相关阅读:
    yii中的若干问题
    http请求头和响应头
    http 协议
    Jmeter -- 对并发认识的误区
    Linux top常用操作
    linux 查看系统性能
    Jmeter -- 循环控制器和线程并发(关注执行顺序)
    Jmeter -- 定时器
    性能指标:TPS 并发数 响应时间 QPS
    Jmeter -- 元件的作用域与执行顺序
  • 原文地址:https://www.cnblogs.com/dongxixi/p/11014368.html
Copyright © 2011-2022 走看看