Python操作excel

一、安装xlrd模块

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

二、使用介绍

1、导入模块

import xlrd

2、打开Excel文件读取数据

data = xlrd.open_workbook('excelFile.xls')

3、使用技巧

获取一个工作表


        table = data.sheets()[0]   # 通过索引顺序获取
 
        table = data.sheet_by_index(0)   # 通过索引顺序获取

        table = data.sheet_by_name(u'Sheet1')   # 通过名称获取
获取整行和整列的值(数组)
         table.row_values(i)
 
         table.col_values(i)

获取行数和列数

        nrows = table.nrows
 
        ncols = table.ncols
循环行列表数据
        for i in range(nrows ):
            print table.row_values(i)
单元格
cell_A1 = table.cell(0,0).value
 
cell_C4 = table.cell(2,3).value
使用行列索引
cell_A1 = table.row(0)[0].value
 
cell_A2 = table.col(1)[0].value
简单的写入
import xlrd

data = xlrd.open_workbook('..\DomainFlow.xls')   # excel存放位置及名字
table = data.sheet_by_name(u'域名分流')   # Excel文件里的表名
# 获取行列数
nrows = table.nrows
ncols = table.ncols
# 循环显示
for i in range(nrows):
    print(table.row_values(i))
原文地址:https://www.cnblogs.com/APeng2019/p/10719387.html