• font(字体类):字号、字体颜色、下划线等
  • fill(填充类):颜色等
  • border(边框类):设置单元格边框
  • alignment(位置类):对齐方式
  • number_format(格式类):数据格式
  • protection(保护类):写保护

>>> from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
>>> font = Font(name='Calibri',
                 size=11,
                 bold=False,
                 italic=False,
                 vertAlign=None,
                 underline='none',
                 strike=False,
                 color='FF000000')
>>> fill = PatternFill(fill_type=None,
                 start_color='FFFFFFFF',
                 end_color='FF000000')
>>> border = Border(left=Side(border_style=None,
                           color='FF000000'),
                 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')
                )
>>> alignment=Alignment(horizontal='general',
                     vertical='bottom',
                     text_rotation=0,
                     wrap_text=False,
                     shrink_to_fit=False,
                     indent=0)
>>> number_format = 'General'
>>> protection = Protection(locked=True,
                         hidden=False)

以上几种样式(字体、填充、边框、位置和保护)实例一旦被创建实例的属性就不可更改,只能重新创建实例。


对一个cell进行格式设置:

ws["B5"].font = ft
ws["B5"].fill =fill
ws["B5"].border = bd
ws["B5"].alignment = alignment
ws["B5"].number_format = number_format
ws["B5"].value ="pynote.net"

编辑页面设置

ws.page_setup.orientation = ws.ORIENTATION_LANDSCAPE
ws.page_setup.paperSize = ws.PAPERSIZE_TABLOID
ws.page_setup.fitToHeight = 0
ws.page_setup.fitToWidth = 1

Re:

https://blog.csdn.net/aishenghuomeidaoli/article/details/52165305

https://www.pynote.net/archives/2229