python openpyxl 封装Execl常用操作的方法

封装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']

 

猜你喜欢

转载自www.cnblogs.com/xiaxiaoxu/p/9261045.html