python openpyxl 2.5.4 版本 excel常用操作封裝

來源:互聯網
上載者:User

標籤:技術分享   value   .sh   ret   src   all_rows   pat   style   name   

最近搭架構用的openpyxl 2.5.4版本,之前封裝的函數有些提示不推薦使用了,我做了一些更新:

 

代碼:

# 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 get_sheet_by_index(self, sheet_index):
        sheet_name = self.workbook.sheetnames[sheet_index]
        self.sheet = self.get_sheet_by_name(sheet_name)
        return self.sheet

    # 擷取當前預設sheet的名字
    def get_default_sheet(self):
        return self.sheet.title

    # 設定當前要操作的sheet對象,使用sheet名稱來擷取相應的sheet
    def get_sheet_by_name(self, sheet_name):
        self.sheet = self.workbook[sheet_name]
        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.get_sheet_by_index(1)
    print u"擷取預設sheet:", p.get_default_sheet()
    print u"設定sheet索引為0", p.get_sheet_by_index(0)
    print u"擷取預設sheet:", p.get_default_sheet()
    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)

 

結果:ok

 

python openpyxl 2.5.4 版本 excel常用操作封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.