zoukankan      html  css  js  c++  java
  • 学习Python操作Excel文件(1)

    1 #! /usr/bin/env python
    2 #coding=utf-8
    3  from xlrd import open_workbook #导入xlrd中的模块open_workbook
    4  wb = open_workbook("Book1.xls") #打开一个xls文件,并赋值给wb
    5  print wb.nsheets #打印这个xls文件的sheet数
    6  print wb.sheet_names() #用unicode格式返回xls文件中所有sheet的名称
    7  for sheet in wb.sheets(): #迭代
    8   print sheet #打印结果
    9   # <xlrd.sheet.Sheet object at 0x01BDCAD0>
    10   # <xlrd.sheet.Sheet object at 0x01BDCC70>
    11   # <xlrd.sheet.Sheet object at 0x01BDCC90>
    12  for sheet_index in range(wb.nsheets): #迭代
    13   print wb.sheet_by_index(sheet_index) #打印结果同上
    14  
    15 for sheet_name in wb.sheet_names(): #迭代
    16 print wb.sheet_by_name(sheet_name) #打印结果同上
    1 #coding:gb2312
    2 from xlrd import open_workbook,cellname
    3 wb = open_workbook("Book1.xls")
    4 print wb.nsheets
    5
    6 for aa in range(wb.nsheets): #打印所有的sheet的名称
    7 sheet = wb.sheet_by_index(aa)
    8 print sheet.name
    9
    10
    11 sheet1 = wb.sheet_by_index(0) #打印index为0的sheet的名称
    12 print sheet1.name
    13
    14
    15 print sheet1.nrows #打印sheet1的总行数
    16 print sheet1.ncols #打印sheet1的总列数
    17
    18 for row_index in range(sheet1.nrows): #迭代每一行
    19 for col_index in range(sheet1.ncols): #迭代每一列
    20 print cellname(row_index ,col_index ),'-', #cellname()为单元格的名称
    21 print sheet1.cell(row_index ,col_index ).value #cell()为单元格的内容
    22
  • 相关阅读:
    SPI总线介绍
    linuxok6410的I2C驱动分析---用户态驱动
    Centos下安装Mongodb
    python学习笔记(二):python数据类型
    python学习笔记(三):文件操作和集合
    python实现显示安装进度条
    python判断一个字符串是否是小数
    python学习笔记(四):函数
    python学习笔记(五):装饰器、生成器、内置函数、json
    python学习笔记(七):面向对象编程、类
  • 原文地址:https://www.cnblogs.com/dabiao/p/1693273.html
Copyright © 2011-2022 走看看