我的第一個python需求,用openpyxl操作excel

需求內容在這裏插入圖片描述

如上表,騎手ID是唯一識別騎手的標誌,我們需要統計下表中每個騎手總共送了多少外賣,並生成到新的一個sheet中。結果如下圖所示:
在這裏插入圖片描述
除過上面的需求外,還需要統計原始的表格數據中,騎手等級和騎手分層狀況,結果如下表所示:
在這裏插入圖片描述
代碼如下

# -*- coding: utf-8 -*-
# @Time : 2020-04-06
# @Author : HXY
# @File : auto_excel.py

from openpyxl import load_workbook

# ID所在的列
DEFAULT_ID_COLUMN_NUM = 1

def count_ids():
    """
    統計騎手ID,並去重,拷貝到第二個表格
    """

    # 獲取原始數據表中的的ids並去重
    workbook = load_workbook(filename='data.xlsx')
    sheet = workbook.active
    ids = []
    for cell in sheet['A']:
        if((cell.value is not None) and (cell.value not in ids)):
            ids.append(cell.value)

    # 創建統計結果sheet,寫入id列
    workbook.create_sheet('統計結果1')
    count_result_sheet = workbook['統計結果1']
    index = 0
    for id in ids:
        index += 1
        count_result_sheet.cell(index, DEFAULT_ID_COLUMN_NUM).value = id

    # 填充姓名
    count_result_sheet['B1'] = '姓名'
    for index in range(2, len(ids) + 1):
        column_id = 'A' + str(index)
        column_name = 'B'+ str(index)
        count_result_sheet[column_name] = '=VLOOKUP(' + column_id + ',表格數據!A:B,2,0)'
       
        
    # 統計當天完成訂單總和
    count_result_sheet['C1'] = '當天外賣完成單'
    for index in range(2, len(ids) + 1):
        column = 'C' + str(index)
        count_result_sheet[column] = '=SUMIFS(表格數據!D:D,表格數據!B:B,表格數據!B' + str(index) + ')'
    
    # 填充騎手等級
    count_result_sheet['D1'] = '騎手等級'
    for index in range(2, len(ids) + 1):
        column_id = 'A' + str(index)
        column_level = 'D' + str(index)
        count_result_sheet[column_level] = '=VLOOKUP(' + column_id + ',表格數據!A:F,5,0)'

    # 填充騎手分層
    count_result_sheet['E1'] = '騎手分層'
    for index in range(2, len(ids) + 1):
        column_id = 'A' + str(index)
        column_layer = 'E' + str(index)
        count_result_sheet[column_layer] = '=vLOOKUP(' + column_id + ',表格數據!A:F,6,0)'

    # 填充騎手等級
    count_result_sheet['F1'] = '騎手等級'
    for index in range(2, len(ids) + 1):
        column_origin = 'D' + str(index)
        column_level = 'F' + str(index)
        count_result_sheet[column_level] = '=MID(' + column_origin + ',3,2)'

    # 統計騎手等級,不知道爲啥跨sheet,countif函數不好用
    # 統計騎手等級
    count_result_sheet['G1'] = '騎手等級'
    count_result_sheet['G2'] = '青銅'
    count_result_sheet['G3'] = '白銀'
    count_result_sheet['G4'] = '黃金'
    count_result_sheet['G5'] = '王者'
    count_result_sheet['H1'] = '合計'
    count_result_sheet['H2'] = '=COUNTIF(F:F,G2)'
    count_result_sheet['H3'] = '=COUNTIF(F:F,G3)'
    count_result_sheet['H4'] = '=COUNTIF(F:F,G4)'
    count_result_sheet['H5'] = '=COUNTIF(F:F,G5)'

    for col in ['F', 'G', 'H']:
        count_result_sheet.column_dimensions[col].hidden = True
    
    # 保存文件
    workbook.save(filename='data.xlsx')


def count_other():
    """
    統計騎手等級和分層
    """

    workbook = load_workbook(filename='data.xlsx')
    workbook.create_sheet('統計結果2')
    count_rider_sheet = workbook['統計結果2']

    # 統計騎手等級
    count_rider_sheet['A1'] = '騎手等級'
    count_rider_sheet['A2'] = '青銅'
    count_rider_sheet['A3'] = '白銀'
    count_rider_sheet['A4'] = '黃金'
    count_rider_sheet['A5'] = '王者'
    count_rider_sheet['B1'] = '合計'
    count_rider_sheet['B2'] = '=統計結果1!H2'
    count_rider_sheet['B3'] = '=統計結果1!H3'
    count_rider_sheet['B4'] = '=統計結果1!H4'
    count_rider_sheet['B5'] = '=統計結果1!H5'

    # 統計騎手分層
    count_rider_sheet['C1'] = '騎手分層(衆包)'
    count_rider_sheet['C2'] = '全能騎手'
    count_rider_sheet['C3'] = '效能短板騎手'
    count_rider_sheet['C4'] = '出勤短板騎手'
    count_rider_sheet['C5'] = '時長短板騎手'
    count_rider_sheet['C6'] = '尾部騎手'
    count_rider_sheet['D1'] = '合計'
    count_rider_sheet['D2'] = '=COUNTIF(表格數據!F:F,C2)'
    count_rider_sheet['D3'] = '=COUNTIF(表格數據!F:F,C3)'
    count_rider_sheet['D4'] = '=COUNTIF(表格數據!F:F,C4)'
    count_rider_sheet['D5'] = '=COUNTIF(表格數據!F:F,C5)'
    count_rider_sheet['D6'] = '=COUNTIF(表格數據!F:F,C6)'

    workbook.save(filename='data.xlsx')

count_ids()
count_other()

剛開始學python,歡迎看到的同學批評指正。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章