字体

Font(name=None, sz=None, b=None, i=None, charset=None, u=None, strike=None, color=None, scheme=None, family=None, size=None, bold=None, italic=None, strikethrough=None, underline=None, vertAlign=None, outline=None, shadow=None, condense=None, extend=None)

  1. from openpyxl.styles import Font, Alignment, Border, Side, PatternFill
  2. cell.font = Font(name='Times New Roman', bold=True, color='FF0000')

填充

PatternFill(patternType=None, fgColor=Color(), bgColor=Color(), fill_type=None, start_color=None, end_color=None)

  1. yellow = 'FFFF00'
  2. cell.fill = PatternFill(start_color=yellow, end_color=yellow, fill_type='solid')

对齐

Alignment(horizontal=None, vertical=None, textRotation=0, wrapText=None, shrinkToFit=None, indent=0, relativeIndent=0, justifyLastLine=None, readingOrder=0, text_rotation=None, wrap_text=None, shrink_to_fit=None, mergeCell=None)

  1. cell.alignment = Alignment(horizontal='center', vertical='center')
  2. # 自动换行
  3. cell.alignment = Alignment(horizontal='left',vertical='center', wrap_text=True)

边框

Border(left=Side(), right=Side(), top=Side(), bottom=Side(), diagonal=Side(), diagonal_direction=None, vertical=None, horizontal=None, diagonalUp=False, diagonalDown=False, outline=True, start=None, end=None)

  1. thin = Side(style='thin', color='CCCCCC')
  2. cell.border = Border(top=thin, left=thin, right=thin, bottom=thin)