封装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. long()

    long() 用于将一个对象转换为长整数 In [35]: long(') # 将纯数字的字符串转换为长整数 Out[35]: 123L In [36]: long(12.3) # 将浮点数转换为长整 ...

  2. 如何使用css影藏滚动条

    1.单纯的一句代码: div ::-webkit-scrollbar {width: 0px;}//或者display:none 但是这代码最大的弊端就是只能在webkit内核的浏览器上进行显示,无法 ...

  3. Spring学习笔记--Spring表达式语言SpEL

    Spring3引入了Spring表达式语言(Spring Expression Language,SpEL).SpEL是一种强大的.简洁的装配Bean的方式,它通过运行期执行的表达式将值装配到Bean ...

  4. java基础---->java中国际化的实现

    应用程序的功能和代码设计考虑在不同地区运行的需要,其代码简化了不同本地版本的生产.开发这样的程序的过程,就称为国际化.今天,我们就开始学习java中国际化的代码实现. Java国际化主要通过如下3个类 ...

  5. JZOJ.5328【NOIP2017模拟8.22】世界线

    Description

  6. 微软 IIS HTTP.sys漏洞原理学习以及POC

    零.MS15-034POC核心部分(参考巡风): socket.setdefaulttimeout(timeout) s = socket.socket(socket.AF_INET, socket. ...

  7. 次小生成树(poj1679)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20737   Accepted: 7281 D ...

  8. mysql客户端不能插入中文字符

    问题:输入中文报错:Incorrect string value 步骤: 1.查看MySQL编码设置 show variables like '%character%'; 2.重新设置编码(注意:ut ...

  9. 170705、springboot编程之自定义properties

    spring boot使用application.properties默认了很多配置.但需要自己添加一些配置的时候,可以这样用,如下! 在application.properties文件中增加信息 1 ...

  10. pta 习题集5-18 打印学生选课清单

    假设全校有最多40000名学生和最多2500门课程.现给出每门课的选课学生名单,要求输出每个前来查询的学生的选课清单. 输入格式: 输入的第一行是两个正整数:N(≤≤40000),为前来查询课表的学生 ...