openpyxl 設置單元格顏色

在處理excel數據格式的時候,須要對特定單元格進行顏色標註,方便相關人員查看 醒目python

# -*- coding: utf-8 -*-ide

from openpyxl import load_workbook, Workbook
# 導入字體、邊框、顏色以及對齊方式相關庫
from openpyxl.styles import Font, Border, Side, PatternFill, colors, Alignment字體

有關顏色的設置具體能夠查看  http://www.114la.com/other/rgb.htmspa

def MarkRedOverTime():
    '''對上週已開船和未開船 超時數據字段進行標紅'''
    try:
        abs_file = os.path.abspath(os.path.join(
            os.path.dirname(__file__), outfile))
        wb = load_workbook(abs_file)
        # 獲取工做表列表
        sheets = wb.sheetnames
        print(sheets)
        # 獲取某一特定的工做表
        # 上一週新增的可發訂單已開船數據  上一週新增的可發訂單未開船數據
        #
        # 上一週新增的可發訂單已開船數據ws = wb["上一週新增的可發訂單未開船數據"]

        # # 設置填充紅色加粗
        red_fill = PatternFill("solid", fgColor="FF0000")
        # # 遍歷每一行
        # for index, row in enumerate(ws.rows):
        #     if index > 0:
        #         # 單證工做時間(時) > 24
        #         cell6 = row[5]
        #         print(cell6.value)
        #         if int(cell6.value) > 24:
        #             cell6.fill = red_fill
        #         # 採購工做時間(時) > 48
        #         cell9 = row[8]
        #         if int(cell9.value) > 48:
        #             cell9.fill = red_fill
        #         # 訂艙用時(時) > 48
        #         cell12 = row[11]
        #         if int(cell12.value) > 48:
        #             cell12.fill = red_fill
        #         # booking是否超時(天) < 0
        #         cell14 = row[13]
        #         if int(cell14.value) < 0:
        #             cell14.fill = red_fill
        wt = wb["上一週新增的可發訂單已開船數據"]
        # 遍歷每一行
        for index, row in enumerate(wt.rows):
            if index > 0:
                # 單證工做時間(時) > 24
                cell6 = row[5]
                if int(cell6.value if cell6.value else "0") > 24:
                    cell6.fill = red_fill
                # 採購工做時間(時) > 48
                cell9 = row[8]
                if int(cell9.value if cell9.value else "0") > 48:
                    cell9.fill = red_fill
                # 訂艙用時(時) > 48
                cell12 = row[11]
                if int(cell12.value if cell12.value else "0") > 48:
                    cell12.fill = red_fill
                # booking是否超時(天) < 0
                cell14 = row[13]
                if int(cell14.value if cell14.value else "0") < 0:
                    cell14.fill = red_fill
                # shipping是否超時(天) < 0
                cell18 = row[17]
                if int(cell18.value if cell18.value else "0") < 0:
                    cell18.fill = red_fill
        wb.save(abs_file)
    except Exception as error_msg:
        print(error_msg)
        fs = traceback.format_exc()
        print(fs)
相關文章
相關標籤/搜索