封装Excel操作方法:
先装openpyxl:pip install openpyxl==2.4.5(可以指定版本)

封装脚本:
#encoding=utf-8

from openpyxl import load_workbook
from openpyxl.styles import Border,Side,Font
import time

class parseExcel(object):
    def __init__(self,excelPath):
        self.excelPath=excelPath
        self.workbook = load_workbook(excelPath)#加载excel
        self.sheet=self.workbook.active#获取第一个sheet
        self.font=Font(color=None)
        self.colorDict={"red":'FFFF3030',"green":'FF008B00'}
    
    #设置当前要操作的sheet对象,使用index来获取相应的sheet
    def set_sheet_by_index(self,sheet_index):
        sheet_name=self.workbook.get_sheet_names()[sheet_index]
      
        self.sheet=self.workbook.get_sheet_by_name(sheet_name)
        return self.sheet

#获取当前默认sheet的名字
    def get_default_sheet(self):
        return self.sheet.title

# 设置当前要操作的sheet对象,使用sheet名称来获取相应的sheet
    def set_sheet_by_name(self,sheet_name):
        sheet=self.workbook.get_sheet_by_name(sheet_name)
        self.sheet=sheet
        return self.sheet

#获取默认sheet中最大的行数
    def get_max_row_no(self):
        return self.sheet.max_row

#获取默认 sheet 的最大列数
    def get_max_col_no(self):
        return self.sheet.max_column

#获取默认sheet的最小(起始)行号
    def get_min_row_no(self):
        return self.sheet.min_row

# 获取默认sheet的最小(起始)列号
    def get_min_col_no(self):
        return self.sheet.min_column

# 获取默认 sheet 的所有行对象,
    def get_all_rows(self):
        return list(self.sheet.iter_rows())
        #return list(self.rows)也可以
    
    #获取默认sheet中的所有列对象
    def get_all_cols(self):
        return list(self.sheet.iter_cols())
        #return list(self.sheet.columns)也可以

#从默认sheet中获取某一列,第一列从0开始
    def get_single_col(self,col_no):
        return self.get_all_cols()[col_no]

#从默认sheet中获取某一行,第一行从0开始
    def get_single_row(self,row_no):
        return self.get_all_rows()[row_no]

#从默认sheet中,通过行号和列号获取指定的单元格,注意行号和列号从1开始
    def get_cell(self,row_no,col_no):
        return self.sheet.cell(row=row_no,column=col_no)

# 从默认sheet中,通过行号和列号获取指定的单元格中的内容,注意行号和列号从1开始
    def get_cell_content(self,row_no,col_no):
        return self.sheet.cell(row=row_no,column=col_no).value

# 从默认sheet中,通过行号和列号向指定单元格中写入指定内容,注意行号和列号从1开始
    # 调用此方法的时候,excel不要处于打开状态
    def write_cell_content(self,row_no,col_no,content,font=None):
        
        self.sheet.cell(row=row_no,column=col_no).value=content
        self.workbook.save(self.excelPath)
        return self.sheet.cell(row=row_no,column=col_no).value

# 从默认sheet中,通过行号和列号向指定单元格中写入当前日期,注意行号和列号从1开始
    #调用此方法的时候,excel不要处于打开状态
    def write_cell_current_time(self,row_no,col_no):
        time1=time.strftime("%Y-%m-%d %H:%M:%S")
        self.sheet.cell(row=row_no,column=col_no).value=str(time1)
        self.workbook.save(self.excelPath)
        return self.sheet.cell(row=row_no,column=col_no).value

def save_excel_file(self):
        self.workbook.save(self.excelPath)

if __name__=='__main__':
    p=parseExcel(u'D:\\testdata.xlsx')
    print u"获取默认行:",p.get_default_sheet()
    
    print u"设置sheet索引为1",p.set_sheet_by_index(1)
    print u"获取默认行:",p.get_default_sheet()
    print u"设置sheet索引为0",p.set_sheet_by_index(0)
    print u"获取默认行:",p.get_default_sheet()
    #for i in range(3,6):
        #for j in range(3,6):
            #p.write_cell_content(i,j,str((i,j)))
    
    print u"最大行数:",p.get_max_row_no()
    print u"最大列数:",p.get_max_col_no()
    print u"最小起始行数:",p.get_min_row_no()
    print u"最小起始列数:",p.get_min_col_no()
    print u"所有行对象:",p.get_all_rows()
    print u"所有列对象:",p.get_all_cols()
    print u"获取某一列(2):",p.get_single_col(2)
    print u"获取某一行(4):",p.get_single_row(4)
    print u"取得行号和列号(2,2)单元格:",p.get_cell(2,2)
    print u"取得行号和列号单元格的内容(2,2)",p.get_cell_content(2,2)
    print u"行号和列号写入内容(11,11):'xiaxiaoxu'",p.write_cell_content(11,11,'xiaxiaoxu')#
    print u"行号和列号写入当前日期(13,13):",p.write_cell_current_time(13,13)

testdata.xlsx运行前:

结果:

D:\test>python test2.py
获取默认行:  aone
设置sheet索引为1 <Worksheet "two">
获取默认行: two
设置sheet索引为0 <Worksheet " aone">
获取默认行:  aone
最大行数: 13
最大列数: 13
最小起始行数: 3
最小起始列数: 3
所有行对象: [(<Cell u' aone'.A1>, <Cell u' aone'.B1>, <Cell u' aone'.C1>, <Cell u' aone'.D1>, <Cell u' aone'.E1>, <Cell u' aone'.F1>, <Cell u' aone'.G1>, <Cell u' aone'.H1>, <Cell u' aone'.I1>, <Cell u' aone'.J1>, <Cell u' aone'.K1>, <Cell u' aone'.L1>, <Cell u' aone'.M1>), (<Cell u' aone'.A2>, <Cell u' aone'.B2>, <Cell u' aone'.C2>, <Cell u' aone'.D2>, <Cell u' aone'.E2>, <Cell u' aone'.F2>, <Cell u' aone'.G2>, <Cell u' aone'.H2>, <Cell u' aone'.I2>, <Cell u' aone'.J2>, <Cell u' aone'.K2>, <Cell u' aone'.L2>, <Cell u' aone'.M2>), (<Cell u' aone'.A3>, <Cell u' aone'.B3>, <Cell u' aone'.C3>, <Cell u' aone'.D3>, <Cell u' aone'.E3>, <Cell u' aone'.F3>, <Cell u' aone'.G3>, <Cell u' aone'.H3>, <Cell u' aone'.I3>, <Cell u' aone'.J3>, <Cell u' aone'.K3>, <Cell u' aone'.L3>, <Cell u' aone'.M3>), (<Cell u' aone'.A4>, <Cell u' aone'.B4>, <Cell u' aone'.C4>, <Cell u' aone'.D4>, <Cell u' aone'.E4>, <Cell u' aone'.F4>, <Cell u' aone'.G4>, <Cell u' aone'.H4>, <Cell u' aone'.I4>, <Cell u' aone'.J4>, <Cell u' aone'.K4>, <Cell u' aone'.L4>, <Cell u' aone'.M4>), (<Cell u' aone'.A5>, <Cell u' aone'.B5>, <Cell u' aone'.C5>, <Cell u' aone'.D5>, <Cell u' aone'.E5>, <Cell u' aone'.F5>, <Cell u' aone'.G5>, <Cell u' aone'.H5>, <Cell u' aone'.I5>, <Cell u' aone'.J5>, <Cell u' aone'.K5>, <Cell u' aone'.L5>, <Cell u' aone'.M5>), (<Cell u' aone'.A6>, <Cell u' aone'.B6>, <Cell u' aone'.C6>, <Cell u' aone'.D6>, <Cell u' aone'.E6>, <Cell u' aone'.F6>, <Cell u' aone'.G6>, <Cell u' aone'.H6>, <Cell u' aone'.I6>, <Cell u' aone'.J6>, <Cell u' aone'.K6>, <Cell u' aone'.L6>, <Cell u' aone'.M6>), (<Cell u' aone'.A7>, <Cell u' aone'.B7>, <Cell u' aone'.C7>, <Cell u' aone'.D7>, <Cell u' aone'.E7>, <Cell u' aone'.F7>, <Cell u' aone'.G7>, <Cell u' aone'.H7>, <Cell u' aone'.I7>, <Cell u' aone'.J7>, <Cell u' aone'.K7>, <Cell u' aone'.L7>, <Cell u' aone'.M7>), (<Cell u' aone'.A8>, <Cell u' aone'.B8>, <Cell u' aone'.C8>, <Cell u' aone'.D8>, <Cell u' aone'.E8>, <Cell u' aone'.F8>, <Cell u' aone'.G8>, <Cell u' aone'.H8>, <Cell u' aone'.I8>, <Cell u' aone'.J8>, <Cell u' aone'.K8>, <Cell u' aone'.L8>, <Cell u' aone'.M8>), (<Cell u' aone'.A9>, <Cell u' aone'.B9>, <Cell u' aone'.C9>, <Cell u' aone'.D9>, <Cell u' aone'.E9>, <Cell u' aone'.F9>, <Cell u' aone'.G9>, <Cell u' aone'.H9>, <Cell u' aone'.I9>, <Cell u' aone'.J9>, <Cell u' aone'.K9>, <Cell u' aone'.L9>, <Cell u' aone'.M9>), (<Cell u' aone'.A10>, <Cell u' aone'.B10>, <Cell u' aone'.C10>, <Cell u' aone'.D10>, <Cell u' aone'.E10>, <Cell u' aone'.F10>, <Cell u' aone'.G10>, <Cell u' aone'.H10>, <Cell u' aone'.I10>, <Cell u' aone'.J10>, <Cell u' aone'.K10>, <Cell u' aone'.L10>, <Cell u' aone'.M10>), (<Cell u' aone'.A11>, <Cell u' aone'.B11>, <Cell u' aone'.C11>, <Cell u' aone'.D11>, <Cell u' aone'.E11>, <Cell u' aone'.F11>, <Cell u' aone'.G11>, <Cell u' aone'.H11>, <Cell u' aone'.I11>, <Cell u' aone'.J11>, <Cell u' aone'.K11>, <Cell u' aone'.L11>, <Cell u' aone'.M11>), (<Cell u' aone'.A12>, <Cell u' aone'.B12>, <Cell u' aone'.C12>, <Cell u' aone'.D12>, <Cell u' aone'.E12>, <Cell u' aone'.F12>, <Cell u' aone'.G12>, <Cell u' aone'.H12>, <Cell u' aone'.I12>, <Cell u' aone'.J12>, <Cell u' aone'.K12>, <Cell u' aone'.L12>, <Cell u' aone'.M12>), (<Cell u' aone'.A13>, <Cell u' aone'.B13>, <Cell u' aone'.C13>, <Cell u' aone'.D13>, <Cell u' aone'.E13>, <Cell u' aone'.F13>, <Cell u' aone'.G13>, <Cell u' aone'.H13>, <Cell u' aone'.I13>, <Cell u' aone'.J13>, <Cell u' aone'.K13>, <Cell u' aone'.L13>, <Cell u' aone'.M13>)]
所有列对象: [(<Cell u' aone'.A1>, <Cell u' aone'.A2>, <Cell u' aone'.A3>, <Cell u' aone'.A4>, <Cell u' aone'.A5>, <Cell u' aone'.A6>, <Cell u' aone'.A7>, <Cell u' aone'.A8>, <Cell u' aone'.A9>, <Cell u' aone'.A10>, <Cell u' aone'.A11>, <Cell u' aone'.A12>, <Cell u' aone'.A13>), (<Cell u' aone'.B1>, <Cell u' aone'.B2>, <Cell u' aone'.B3>, <Cell u' aone'.B4>, <Cell u' aone'.B5>, <Cell u' aone'.B6>, <Cell u' aone'.B7>, <Cell u' aone'.B8>, <Cell u' aone'.B9>, <Cell u' aone'.B10>, <Cell u' aone'.B11>, <Cell u' aone'.B12>, <Cell u' aone'.B13>), (<Cell u' aone'.C1>, <Cell u' aone'.C2>, <Cell u' aone'.C3>, <Cell u' aone'.C4>, <Cell u' aone'.C5>, <Cell u' aone'.C6>, <Cell u' aone'.C7>, <Cell u' aone'.C8>, <Cell u' aone'.C9>, <Cell u' aone'.C10>, <Cell u' aone'.C11>, <Cell u' aone'.C12>, <Cell u' aone'.C13>), (<Cell u' aone'.D1>, <Cell u' aone'.D2>, <Cell u' aone'.D3>, <Cell u' aone'.D4>, <Cell u' aone'.D5>, <Cell u' aone'.D6>, <Cell u' aone'.D7>, <Cell u' aone'.D8>, <Cell u' aone'.D9>, <Cell u' aone'.D10>, <Cell u' aone'.D11>, <Cell u' aone'.D12>, <Cell u' aone'.D13>), (<Cell u' aone'.E1>, <Cell u' aone'.E2>, <Cell u' aone'.E3>, <Cell u' aone'.E4>, <Cell u' aone'.E5>, <Cell u' aone'.E6>, <Cell u' aone'.E7>, <Cell u' aone'.E8>, <Cell u' aone'.E9>, <Cell u' aone'.E10>, <Cell u' aone'.E11>, <Cell u' aone'.E12>, <Cell u' aone'.E13>), (<Cell u' aone'.F1>, <Cell u' aone'.F2>, <Cell u' aone'.F3>, <Cell u' aone'.F4>, <Cell u' aone'.F5>, <Cell u' aone'.F6>, <Cell u' aone'.F7>, <Cell u' aone'.F8>, <Cell u' aone'.F9>, <Cell u' aone'.F10>, <Cell u' aone'.F11>, <Cell u' aone'.F12>, <Cell u' aone'.F13>), (<Cell u' aone'.G1>, <Cell u' aone'.G2>, <Cell u' aone'.G3>, <Cell u' aone'.G4>, <Cell u' aone'.G5>, <Cell u' aone'.G6>, <Cell u' aone'.G7>, <Cell u' aone'.G8>, <Cell u' aone'.G9>, <Cell u' aone'.G10>, <Cell u' aone'.G11>, <Cell u' aone'.G12>, <Cell u' aone'.G13>), (<Cell u' aone'.H1>, <Cell u' aone'.H2>, <Cell u' aone'.H3>, <Cell u' aone'.H4>, <Cell u' aone'.H5>, <Cell u' aone'.H6>, <Cell u' aone'.H7>, <Cell u' aone'.H8>, <Cell u' aone'.H9>, <Cell u' aone'.H10>, <Cell u' aone'.H11>, <Cell u' aone'.H12>, <Cell u' aone'.H13>), (<Cell u' aone'.I1>, <Cell u' aone'.I2>, <Cell u' aone'.I3>, <Cell u' aone'.I4>, <Cell u' aone'.I5>, <Cell u' aone'.I6>, <Cell u' aone'.I7>, <Cell u' aone'.I8>, <Cell u' aone'.I9>, <Cell u' aone'.I10>, <Cell u' aone'.I11>, <Cell u' aone'.I12>, <Cell u' aone'.I13>), (<Cell u' aone'.J1>, <Cell u' aone'.J2>, <Cell u' aone'.J3>, <Cell u' aone'.J4>, <Cell u' aone'.J5>, <Cell u' aone'.J6>, <Cell u' aone'.J7>, <Cell u' aone'.J8>, <Cell u' aone'.J9>, <Cell u' aone'.J10>, <Cell u' aone'.J11>, <Cell u' aone'.J12>, <Cell u' aone'.J13>), (<Cell u' aone'.K1>, <Cell u' aone'.K2>, <Cell u' aone'.K3>, <Cell u' aone'.K4>, <Cell u' aone'.K5>, <Cell u' aone'.K6>, <Cell u' aone'.K7>, <Cell u' aone'.K8>, <Cell u' aone'.K9>, <Cell u' aone'.K10>, <Cell u' aone'.K11>, <Cell u' aone'.K12>, <Cell u' aone'.K13>), (<Cell u' aone'.L1>, <Cell u' aone'.L2>, <Cell u' aone'.L3>, <Cell u' aone'.L4>, <Cell u' aone'.L5>, <Cell u' aone'.L6>, <Cell u' aone'.L7>, <Cell u' aone'.L8>, <Cell u' aone'.L9>, <Cell u' aone'.L10>, <Cell u' aone'.L11>, <Cell u' aone'.L12>, <Cell u' aone'.L13>), (<Cell u' aone'.M1>, <Cell u' aone'.M2>, <Cell u' aone'.M3>, <Cell u' aone'.M4>, <Cell u' aone'.M5>, <Cell u' aone'.M6>, <Cell u' aone'.M7>, <Cell u' aone'.M8>, <Cell u' aone'.M9>, <Cell u' aone'.M10>, <Cell u' aone'.M11>, <Cell u' aone'.M12>, <Cell u' aone'.M13>)]
获取某一列(2): (<Cell u' aone'.C1>, <Cell u' aone'.C2>, <Cell u' aone'.C3>, <Cell u' aone'.C4>, <Cell u' aone'.C5>, <Cell u' aone'.C6>, <Cell u' aone'.C7>, <Cell u' aone'.C8>, <Cell u' aone'.C9>, <Cell u' aone'.C10>, <Cell u' aone'.C11>, <Cell u' aone'.C12>, <Cell u' aone'.C13>)
获取某一行(4): (<Cell u' aone'.A5>, <Cell u' aone'.B5>, <Cell u' aone'.C5>, <Cell u' aone'.D5>, <Cell u' aone'.E5>, <Cell u' aone'.F5>, <Cell u' aone'.G5>, <Cell u' aone'.H5>, <Cell u' aone'.I5>, <Cell u' aone'.J5>, <Cell u' aone'.K5>, <Cell u' aone'.L5>, <Cell u' aone'.M5>)
取得行号和列号(2,2)单元格: <Cell u' aone'.B2>
取得行号和列号单元格的内容(2,2) None
行号和列号写入内容(11,11):'xiaxiaoxu' xiaxiaoxu
行号和列号写入当前日期(13,13): 2018-07-03 22:21:18

Testdata.xlsx运行后:

注意点:
self.sheet.active()获取第一个sheet,这个第一个指的sheet名的ascii码第一个字母排在最前的

不熟悉的话可以在命令行界面自己练习一下,用dir()命令查看workbook和sheet的常用的方法:

>>> from openpyxl import *
>>> wb=load_workbook('d:\\testdata.xlsx')
>>> wb.get_sheet_names()
[u' aone', u'two', u'sheet3']
>>> sheet1=wb.active
>>> sheet1
<Worksheet "Sheet3">
>>> sheet1.max_column
8
>>> sheet1.max_row
8
>>> sheet1.min_row
3
>>> sheet1.min_column
3
>>> sheet1.rows
<generator object _cells_by_row at 0x04882AD0>
>>> sheet1.iter_rows()
<generator object _cells_by_row at 0x053F8EB8>
>>> sheet1.columns
<generator object _cells_by_col at 0x053F8EB8>

>>> dir(wb)
['_Workbook__write_only', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_active_sheet_index', '_add_sheet', '_alignments', '_borders', '_cell_styles', '_colors', '_data_only', '_differential_styles', '_external_links', '_fills', '_fonts', '_keep_links', '_named_styles', '_number_formats', '_protections', '_read_only', '_setup_styles', '_sheets', 'active', 'add_named_range', 'add_named_style', 'chartsheets', 'close', 'code_name', 'copy_worksheet', 'create_chartsheet', 'create_named_range', 'create_sheet', 'data_only', 'defined_names', 'encoding', 'excel_base_date', 'get_active_sheet', 'get_index', 'get_named_range', 'get_named_ranges', 'get_sheet_by_name', 'get_sheet_names', 'guess_types', 'index', 'is_template', 'keep_links', 'loaded_theme', 'mime_type', 'named_styles', 'path', 'properties', 'read_only', 'rels', 'remove', 'remove_named_range', 'remove_sheet', 'save', 'security', 'shared_strings', 'sheetnames', 'style_names', 'template', 'vba_archive', 'worksheets', 'write_only']

 

python openpyxl 封装Execl常用操作的方法的更多相关文章

  1. 字符串的常用操作和方法(Python入门教程)

    字符串的常用操作 很好理解 字符串可以用 ' + ' 连接,或者乘一个常数重复输出字符串 字符串的索引操作 通过一对中括号可以找到字符串中的某个字符 可以通过正负数双向操作噢 用一个中括号来实现 为什 ...

  2. Python对 Excel 的常用操作

    几个常用的对象 Workbook:工作簿,一个包含多个Sheet的Excel文件 Worksheet:工作表,一个Workbook有多个Worksheet,如"Sheet1",&q ...

  3. Python列表类型及常用操作

    Python列表类型 1.用途: 存放多个值,可以根据索引存取值 2.定义方式: 在[ ]内用逗号分割开多个任意类型的值 l=['yven','law','lyf'] #l=list(['yven', ...

  4. 超详细!盘点Python中字符串的常用操作

    在Python中字符串的表达方式有四种 一对单引号 一对双引号 一对三个单引号 一对三个双引号 a = 'abc' b= "abc" c = '''abc''' d = " ...

  5. 『无为则无心』Python序列 — 18、Python列表概念及常用操作API

    目录 1.列表的概念 (1)列表的定义 (2)列表的应用场景 (3)列表的定义格式 2.列表的常用操作 (1)列表的查找 1)通过下标查找 2)通过方法查找 3)判断是否存在 (2)列表的增加 @1. ...

  6. Python 基礎 - 字符串常用操作

    字符串常用操作 今天就介紹一下常用的字符串操作,都是以 Python3撰寫的 首字母變大寫 #!/usr/bin/env python3 # -*- coding:utf-8 -*- name = & ...

  7. python基础之列表常用操作及知识点小结

    列表(list) List(列表) 是 Python 中使用最频繁的数据类型.列表可以完成大多数集合类的数据结构实现.它支持字符,数字,字符串甚至可以包含列表(所谓嵌套).列表用[ ]标识,是pyth ...

  8. python数据类型:字典dict常用操作

    字典是Python语言中的映射类型,他是以{}括起来,里面的内容是以键值对的形式储存的: Key: 不可变(可哈希)的数据类型.并且键是唯一的,不重复的. Value:任意数据(int,str,boo ...

  9. Python基础灬文件常用操作

    文件常用操作 文件内建函数和方法 open() :打开文件 read():输入 readline():输入一行 seek():文件内移动 write():输出 close():关闭文件 写文件writ ...

随机推荐

  1. Template类的使用指南【python】

    转自:http://www.jb51.net/article/55011.htm

  2. org.apache.ibatis.builder.IncompleteElementException: Could not find parameter map com.hyzn.historicalRecord.dao.ITB_HISTORYLOGDAO.TB_HISTORYLOGResultMap

    用了很久的myBatis,忽然出现这个错误,感觉配置什么的都是正确的,错误如下: org.apache.ibatis.builder.IncompleteElementException: Could ...

  3. CentOS安装最新版的火狐

    1. 下载最新版的火狐 Firefox-latest.tar.bz2 2.yum -y remove firefox 3.rm -rf /usr/lib/firefox      ##64位的机器为l ...

  4. Facebook广告目标摘要

    Facebook Ads look slightly different depending on the results you want. This guide includes recommen ...

  5. @ResponseBody将集合数据转换为json格式并返回给客户端

    spring-mvc.xml: <beans xmlns:mvc="http://www.springframework.org/schema/mvc" > <m ...

  6. echarts实现柱状图分页展示

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. poj_2441 状态压缩dp

    题目大意 N头牛,M个谷仓,每个牛c都有它喜欢的若干个谷仓,现在要将这N头牛安排进谷仓,使得每个牛都位于它喜欢的谷仓,而每个谷仓只能有一头牛.求安排的方案总数.N, M <= 20 题目分析 将 ...

  8. c++11——改进容器性能

    使用emplace_back就地构造 emplace_back能就地通过参数构造对象,不需要拷贝或者移动内存,相比push_back能更好的避免内存的拷贝和移动,使得容器插入元素的性能得到进一步提升. ...

  9. 【WebService】Stax的基本操作基于游标

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <book ...

  10. 《IT项目经理成长手记》

    下载地址: https://download.csdn.net/download/h623691640/9109265 读后感: 曾担任过试用期3个月项目经理,遇到的问题确实如书中所说,几乎有一些是一 ...