zoukankan      html  css  js  c++  java
  • python28 excel读取模块xlrd

    安装:

    pip install xlrd

    简单使用:

    import xlrd
    
    book = xlrd.open_workbook(r'C:UsersdinghanhuaDesktopyqqapi.xlsx') # 打开excel
    print("the number of sheets:",book.nsheets) # sheet数量
    print("sheet_names:",book.sheet_names()) # sheetname列表
    
    sheet1 = book.sheet_by_index(0) # 通过索引取sheet
    print(sheet1.name,sheet1.nrows,sheet1.ncols) #sheet名称、行数、列数
    print(sheet1.cell(0,0).value) #cell值
    print(sheet1.cell_value(0,1)) #cell值
    
    sheet2 = book.sheet_by_name("sheet2") # 通过sheetname取sheet
    print(sheet2.name,sheet2.nrows,sheet2.ncols)
    
    # 获取sheet所有的数据
    for row in range(sheet1.nrows):
        for col in range(sheet1.ncols):
            print(sheet1.cell_value(row,col),end='	')
        print('
    ')
    
    print(sheet1.col_values(0,1,sheet1.nrows)) # 获取第一列,第2行的所有值
    
    print(sheet1.row(1)) # 获取第二行的值
    
    for col in range(sheet1.ncols): # 按列获取值,每列是list
        print(sheet1.col_values(col,0,sheet1.nrows))
    
    for row in range(sheet1.nrows): # 按行获取值;每行都是list
        print(sheet1.row_values(row,0,sheet1.ncols))

    有的单元格带有左右空格,取值时用strip()处理下

    cell = str(sheet1.cell_value(0,0)).strip() 

    the end!

  • 相关阅读:
    书_Delphi
    20160226
    SVG_style_script
    辅助
    电影_Z
    Windows下软件调试
    20160221
    Qt5.3.2_vs10_发布时所需DLL的路径
    android intent 传数据
    android 消息机制
  • 原文地址:https://www.cnblogs.com/dinghanhua/p/10348076.html
Copyright © 2011-2022 走看看