学习Python操作Excel文件(1)
2010-03-23 22:33
603 查看
#! /usr/bin/env python #coding=utf-8 from xlrd import open_workbook #导入xlrd中的模块open_workbook wb = open_workbook("Book1.xls") #打开一个xls文件,并赋值给wb print wb.nsheets #打印这个xls文件的sheet数 print wb.sheet_names() #用unicode格式返回xls文件中所有sheet的名称 for sheet in wb.sheets(): #迭代 print sheet #打印结果 # <xlrd.sheet.Sheet object at 0x01BDCAD0> # <xlrd.sheet.Sheet object at 0x01BDCC70> # <xlrd.sheet.Sheet object at 0x01BDCC90> for sheet_index in range(wb.nsheets): #迭代 print wb.sheet_by_index(sheet_index) #打印结果同上 for sheet_name in wb.sheet_names(): #迭代 print wb.sheet_by_name(sheet_name) #打印结果同上
#coding:gb2312 from xlrd import open_workbook,cellname wb = open_workbook("Book1.xls") print wb.nsheets for aa in range(wb.nsheets): #打印所有的sheet的名称 sheet = wb.sheet_by_index(aa) print sheet.name sheet1 = wb.sheet_by_index(0) #打印index为0的sheet的名称 print sheet1.name print sheet1.nrows #打印sheet1的总行数 print sheet1.ncols #打印sheet1的总列数 for row_index in range(sheet1.nrows): #迭代每一行 for col_index in range(sheet1.ncols): #迭代每一列 print cellname(row_index ,col_index ),'-', #cellname()为单元格的名称 print sheet1.cell(row_index ,col_index ).value #cell()为单元格的内容
相关文章推荐
- python学习笔记1(字符串操作)
- Python学习之使用Pillow(PIL)进行图像操作方法详解
- 【Python学习】xlwt框架操作excel表格
- Python学习笔记之简单文件操作
- Python 学习入门(9)—— set操作
- Python学习笔记(18)-目录操作与文件读写
- Python学习(21)python操作mysql数据库_操作
- python操作redis学习笔记
- Python学习 - 列表基本操作
- python学习 变量的操作 与 基本数据类型
- Python学习笔记(二):基本数据类型及操作(逻辑、字符串、浮点、复数)
- 学习python之编写简单简单连接数据库并执行查询操作
- Python学习笔记5-字符串、bool、数值操作和数组字典排序
- Python学习:操作MySQL时,怎样把元组作为查询条件,传递过去
- Python学习:对MySQL实现简单的无限极分类操作
- python学习笔记.字典基础操作
- Python 学习开发笔记之IO操作
- Python学习笔记-数据报表之Excel操作模块
- Boost.Python 学习笔记 No.2——python操作c++对象
- Jython-Java操作Python脚本学习笔记(一)