Openpyxl--实践心得

一、设置首行单元格的样式,并且将pass/fail写入excel

    def set_columns_pass_style(self, sheet_name: str, result: str) -> None:
        """set columns style and write the fail/pass results into excel"""
        workbook = openpyxl.load_workbook(sheet_name)
        sheet = workbook.active
        sheet.row_dimensions[1].height = 26
        for cell in sheet[1]:  # Adjust title style
            sheet.column_dimensions[get_column_letter(cell.column)].width = len(cell.value) + 1
            cell.fill = PatternFill(start_color="8DB4E2", end_color="8DB4E2", fill_type="solid")
            cell.font = Font(name="Times New Roman", size=12, bold=True)
            cell.alignment = Alignment(horizontal='center', vertical='center')
        sheet["L3"] = result  # set pass/fail font
        sheet.merge_cells("L3:M7")
        if result == "pass":
            sheet['L3'].fill = PatternFill(start_color="007E1C", end_color="007E"
                                                                           "1C", fill_type="solid")
        else:
            sheet['L3'].fill = PatternFill(start_color="FF0000", end_color="FF0000", fill_type="solid")
        sheet["L3"].font = Font(name="Times New Roman", size=30, bold=True)
        sheet["L3"].alignment = Alignment(horizontal='center', vertical='center')
        workbook.save(sheet_name)

1.设置单元格行高

设置第一行数据的行高

workbook = openpyxl.load_workbook(sheet_name)
        sheet = workbook.active
        sheet.row_dimensions[1].height = 26

2.设置单元格数据列宽自适应

sheet.column_dimensions[get_column_letter(cell.column)].width = len(cell.value) + 1

3.填充单元格颜色

cell.fill = PatternFill(start_color="8DB4E2", end_color="8DB4E2", fill_type="solid")

4.设置字体样式,大小,并加粗显示

 cell.font = Font(name="Times New Roman", size=12, bold=True)

5.设置字体居中显示

cell.alignment = Alignment(horizontal='center', vertical='center')

6.设置合并单元格

sheet.merge_cells("L3:M7")

7.设置多个单元格填充相同的样式

fill_green = PatternFill(start_color="008000", end_color="008000", fill_type="solid")
fill_green_cells = ["R3", "S3", "R6", "S6"]
for cell in fill_green_cells:
    ws[cell].fill = fill_green

二、设置首行格式,并设定内容的判定条件

 def set_judgement(self, sheet_name: str) -> None:
        """set header style and set judgement"""
        workbook = openpyxl.load_workbook(sheet_name)
        ws = workbook.active
        # Adjust title style
        ws.row_dimensions[1].height = 26
        for cell in ws[1]:
            ws.column_dimensions[get_column_letter(cell.column)].width = len(cell.value) + 1
            cell.fill = PatternFill(start_color="8DB4E2", end_color="8DB4E2", fill_type="solid")
            cell.font = Font(name="Times New Roman", size=12, bold=True)
            cell.alignment = Alignment(horizontal='center', vertical='center')
        # set judgement
        ##省略判定内容
        fill_green = PatternFill(start_color="008000", end_color="008000", fill_type="solid")
        fill_yellow = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid")
        fill_red = PatternFill(start_color="FF0000", end_color="FF0000", fill_type="solid")
        font_blue = Font(name="Times New Roman", size=10, color="5569FA", bold=True)
        fill_green_cells = ["R3", "S3", "R6", "S6"]
        fill_red_cells = ["R4", "S4", "R8", "S8", "R10", "S10"]
        fill_yellow_cells = ["R7", "S7"]
        font_blue_cells = ["L3", "T3", "T4", "L6", "T6", "T7", "T8", "L10"]
        for cell in font_blue_cells:
            ws[cell].font = font_blue
        for cell in fill_green_cells:
            ws[cell].fill = fill_green
        for cell in fill_red_cells:
            ws[cell].fill = fill_red
        for cell in fill_yellow_cells:
            ws[cell].fill = fill_yellow
        workbook.save(sheet_name)

你可能感兴趣的:(python,pandas)