def modify_cell_style1(self): """ 修改单元格样式 openpyxl提供的格式控制方法可以实现对单元格属性所有基本操作 :return: """ # 字体格式 # 指定字体类型、大小、是否加粗、颜色等 font0 = Font(name='Calibri', size=20, bold=False, italic=False, vertAlign=None, # Maybe:'baseline', 'superscript', 'subscript' underline='none', # Maybe:'single','double','singleAccounting','doubleAccounting' strike=False, color='FF00FF00') # 单元格填充 fill0 = PatternFill(fill_type=None, # Maybe:'lightUp', 'darkVertical', 'darkGrid', 'solid', 'darkHorizontal', 'darkUp', 'lightVertical', 'lightGray', 'darkTrellis', 'lightDown', 'gray125', 'gray0625', 'mediumGray', 'lightTrellis', 'darkGray', 'darkDown', 'lightHorizontal', 'lightGrid' start_color='FFFFFFFF', end_color='FF000000') # 边框 border0 = Border(left=Side(border_style=None, color='FF000000'), # style Maybe:'mediumDashDotDot', 'dotted', 'thick', 'medium', 'dashDotDot', 'double', 'dashed', 'mediumDashed', 'dashDot', 'mediumDashDot', 'hair', 'slantDashDot', 'thin' right=Side(border_style=None, color='FF000000'), top=Side(border_style=None, color='FF000000'), bottom=Side(border_style=None, color='FF000000'), diagonal=Side(border_style=None, color='FF000000'), diagonal_direction=0, outline=Side(border_style=None, color='FF000000'), vertical=Side(border_style=None, color='FF000000'), horizontal=Side(border_style=None, color='FF000000') ) # 单元格对齐方式 alignment0 = Alignment(horizontal='center', # Maybe:'centerContinuous', 'fill', 'right', 'distributed', 'justify', 'general', 'center', 'left' vertical='bottom', text_rotation=0, wrap_text=False, shrink_to_fit=False, indent=0) # 表格保护 protection0 = Protection(locked=True, hidden=False) # 创建格式 style0 = NamedStyle(name='style_example') # 格式赋值 style0.font = font0 style0.alignment = alignment0 style0.border = border0 style0.fill = fill0 style0.Protection = protection0 wb = openpyxl.Workbook() sheet = wb['Sheet'] write_value_to_cell_with_num(sheet, 1, 1, 'xag') # 设置属性样式 sheet['A1'].font = font0 sheet['A1'].alignment = alignment0 sheet.cell(row=1, column=1).border = border0 # 按名称调用 # sheet['A1'].style = style0 # sheet['A1'].style = 'style_example' # sheet.cell(row=1, column=1).style = style0 wb.template = False wb.save('new.xlsx')
protection0 = Protection(locked=True, hidden=False) # In[3]: # 导入相关模块 from openpyxl.styles import NamedStyle # 创建格式 style0 = NamedStyle(name='style_example0') #格式赋值 style0.font = font0 style0.alignment = alignment0 style0.border = border0 style0.fill = fill0 style0.Protection = protection0 # 创建格式 style1 = NamedStyle(name='style_example1') #格式赋值 style1.font = font1 style1.alignment = alignment0 style1.border = border0 style1.fill = fill1 style1.Protection = protection0 # 创建格式 style2 = NamedStyle(name='style_example2') #格式赋值