python操作excel

1、xlrd、xlwt、openpyxl模块

1.xlrd:对xls等excel文件的读取
2.xlwt:对xls等excel文件的写入,要求单个sheet页不能超过65535行
3.openpyxl:对xlsm、xlsx等excel文件的读写,对文件大小没有限制

2、xlrd读取excel文件

python通过xlrd读取excel的时候,返回的数据类型主要有以下5种:

cell_type = {
        "empty":0,
        "text":1,
        "number":2,
        "date":3,
        "boolean":4
    }

获取指定的sheet页

def get_excel(self,datafile,num=0):
        '''
        获取指定excel的具体sheet页并返回
        :param datafile: excel的路径
        :param num: sheet页的下标索引,默认从0开始
        :return: 返回sheet对象
        '''
        excel = xlrd.open_workbook(datafile)
        sheet = excel.sheet_by_index(num)
        return sheet

获取excel的行数或者列数

def get_all_rows(self,datafile,num=0):
        '''
        获取sheet页的所有行数
        :param datafile: excel的路径
        :param num: sheet页的下标索引,默认从0开始
        :return: 返回行数
        '''
        rows = self.get_excel(datafile,num).nrows
        return rows

获取excel具体单元格的内容,若单元格为日期,获取到的是时间戳,需要进行特殊处理

 def get_cell_value(self, datafile, row, col, num=0):
        '''
        获取具体单元格的内容,若单元格返回为日期类型则需要特殊处理
        :param datafile: excel的路径
        :param row: 需要获取单元格的行
        :param col: 需要获取单元格的列
        :param num: sheet页的下标索引,默认从0开始
        :return: 返回单元格的内容
        '''
        #print(self.get_excel(datafile, num).cell(row, col).ctype)
        cell_value = self.get_excel(datafile, num).cell_value(row, col)
        if self.get_excel(datafile).cell_type(row,col) != self.cell_type["date"]:
            return cell_value
        else:
            delta = datetime.timedelta(days=cell_value)
            ndate = datetime.datetime.strptime('1899-12-30', '%Y-%m-%d') + delta
            fdate = datetime.datetime.strftime(ndate, '%Y-%m-%d')
            return fdate

3、使用xlutils.copy保存excel文件,但是只能保存后缀为xls的文件,不能保存xlsx后缀的文件

 def copy_data(self,datafile,resultfile):
        '''
        将数据先复制到结果excel中
        :param datafile:测试数据的excel
        :param resultfile:结果excel文件
        :return:无返回值
        '''
        excel = xlrd.open_workbook(datafile)
        old_content = copy(excel)
        old_content.save(resultfile)

    def write_resut(self,resultfile,row,content,num=0):
        '''
        将测试结果写入结果excel文件中
        :param resultfile:结果数据excel文件
        :param row:具体写入的行
        :param content:需要写入的内容
        :param num:sheet页的下标索引,默认从0开始
        :return:无返回
        '''
        excel = xlrd.open_workbook(resultfile)
        old_content = copy(excel)
        old_content.get_sheet(num).write(row,self.enum.get_actulResult,content)
        old_content.save(resultfile)

4、xlrd与openpyxl的读取速度差异

由于文件比较小的时候差别不大,这里选则读取第65519行,第4列的单元格内容,利用time.clock()计算时间。

import xlrd
import openpyxl
import time
from common.base import *

def get_xlrd_time():
    time.clock()
    f = xlrd.open_workbook(base_file('data', 'test.xls'))
    s = f.sheet_by_index(0)
    s.cell_value(65519,3)
    t = time.clock()
    print(t)

def get_openpyxl_time():
    time.clock()
    f = openpyxl.load_workbook(base_file('data', 'test.xlsx'))
    s = f['test']
    s.cell(65519,4).value
    t = time.clock()
    print(t)

get_xlrd_time()
get_openpyxl_time()

重复执行五次的结果:


result.png

结论:
整体而言,两种包对小文件的读写速度差别不大,而面对较大文件,xlrd速度明显优于openpyxl,对于大文件更建议使用xlrd读取。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容