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