Python系列 之 openpyxl库 styles包
styles包主要是用openpyxl库来操作Excel的一些样式的操作:
包括:字体设置>>字体大小、颜色、下划线等;填充设置图案或颜色渐变;
在单元格上设置边框;对齐方式等
number_format 数字格式
单元格设置数字格式:
cell.number_format = "General"
number_format 对应格式:
# 'General'
# '0'
# '0.00'
# '#,##0'
# '#,##0.00'
# '"$"#,##0_);("$"#,##0)'
# '"$"#,##0_);[Red]("$"#,##0)'
# '"$"#,##0.00_);("$"#,##0.00)'
# '"$"#,##0.00_);[Red]("$"#,##0.00)'
# '0%'
# '0.00%'
# '0.00E+00'
# '# ?/?'
# '# ??/??'
# 'mm-dd-yy'
# 'd-mmm-yy'
# 'd-mmm'
# 'mmm-yy'
# 'h:mm AM/PM'
# 'h:mm:ss AM/PM'
# 'h:mm'
# 'h:mm:ss'
# 'm/d/yy h:mm'
# '#,##0_);(#,##0)'
# '#,##0_);[Red](#,##0)'
# '#,##0.00_);(#,##0.00)'
# '#,##0.00_);[Red](#,##0.00)'
# r'_(* #,##0_);_(* \(#,##0\);_(* "-"_);_(@_)',
# r'_("$"* #,##0_);_("$"* \(#,##0\);_("$"* "-"_);_(@_)'
# r'_(* #,##0.00_);_(* \(#,##0.00\);_(* "-"??_);_(@_)'
# r'_("$"* #,##0.00_)_("$"* \(#,##0.00\)_("$"* "-"??_)_(@_)'
# 'mm:ss'
# '[h]:mm:ss'
# 'mmss.0',
# '##0.0E+0'
# '@'
number_format 设置数字格式,等于Excel下图设置:
Alignment模块
openpyxl.styles.Alignment模块 用于对齐样式的选项:
# openpyxl.styles.alignment.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)
from openpyxl.styles import Alignment
alig = Alignment()
# 设置水平方向对齐方式
# horizontal 取值范围:
# horizontal = {'right', 'justify', 'fill', 'center', 'general', 'left', 'distributed', 'centerContinuous'}
alig.horizontal = 'right'
# 设置垂直方向对齐方式
#vertical 取值范围:
#vertical = {'bottom', 'justify', 'center', 'top', 'distributed'}
alig.vertical = 'top'
#textRotation | text_rotation 文本旋转 值必须是 0 - 180
alig.text_rotation = 180
# wrapText | wrap_text 是否 自动换行 bool
alig.wrap_text = True
# shrinkToFit | shrink_to_fit 是否缩小字体填充
alig.shrink_to_fit = True
# indent 缩进
alig.indent = 0
# 单元格设置
cell.alignment = alig
对应Excel设置页面:
fonts模块
用于字体设置:
# openpyxl.styles.fonts.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)
# 常用设置:
# name==字体名称
# color==字体颜色=Color
# sz==size==字体大小=float
# b==bold==是否粗体=bool
# i==italic==是否斜体=bool
# u==underline==下划线
# 取值范围: 'single', 'doubleAccounting', 'singleAccounting', 'double'
# strike==strikethrough== 删除线=bool
# vertAlign 取值范围 'superscript', 'baseline', 'subscript'
#superscript==上标
# subscript==下标
from openpyxl.styles import Font
ft = Font(name="黑体", sz=10, b=True, i=True, color=Color(indexed=53))
# 设置字体
cell.font = ft
对应Excel设置
borders模块
borders模块下的两个类:
openpyxl.styles.borders.Border 设置边框
openpyxl.styles.borders.Side 设置线条
对应Excel设置:
from openpyxl.styles.borders import Border, Side
# openpyxl.styles.borders.Side(style=None, color=None, border_style=None)
# style 取值范围:
# style == 'dashDot', 'dashDotDot', 'dashed', 'mediumDashDot', 'double', 'slantDashDot', 'thin', 'hair', 'dotted',
# 'thick', 'mediumDashed', 'mediumDashDotDot', 'medium'
# 设置 线条样式
l_side = Side(style='dashDot', color=Color(indexed=10))
r_side = Side(style='dashDotDot', color=Color(indexed=30))
t_side = Side(style='dashed', color=Color(indexed=40))
b_side = Side(style='mediumDashDot', color=Color(indexed=50))
# openpyxl.styles.borders.Border(
# left=<openpyxl.styles.borders.Side object>
# right=<openpyxl.styles.borders.Side object>
# top=<openpyxl.styles.borders.Side object>
# bottom=<openpyxl.styles.borders.Side object>
# diagonal=<openpyxl.styles.borders.Side object>
# diagonal_direction=None, vertical=None, horizontal=None, diagonalUp=False,
# diagonalDown=False, outline=True, start=None, end=None)
border = Border(left=l_side, right=r_side, top=t_side, bottom=b_side, vertical=l_side)
# 设置样式
cell.border = border
fills模块
openpyxl.styles.fills模块:
openpyxl.styles.fills.GradientFill 用渐变填充区域
openpyxl.styles.fills.PatternFill 用图案填充区域
对应Excel设置:
渐变填充:
# openpyxl.styles.fills.GradientFill(type='linear', degree=0, left=0, right=0, top=0, bottom=0, stop=())
# 支持两种渐变填充
# 1.type='linear'渐变在一个区域的长度上,在一组指定的停止点之间插入颜色
# 2.type='path'渐变从区域的每个边缘应用线性渐变
from openpyxl.styles.fills import GradientFill
cell.fill = GradientFill(type='linear', stop=(Color(indexed=10), Color(indexed=20), Color(indexed=30)))
cell.fill = GradientFill(type='path', stop=(Color(indexed=10), Color(indexed=20), Color(indexed=30)))
图案填充:
# openpyxl.styles.fills.PatternFill 用图案填充区域
# openpyxl.styles.fills.PatternFill(patternType=None, fgColor= < openpyxl.styles.colors.Color object >
# bgColor = < openpyxl.styles.colors.Color object >
# fill_type = None, start_color = None, end_color = None)
# patternType 取值范围:
# 'gray0625', 'gray125', 'solid', 'darkHorizontal', 'lightGrid', 'lightTrellis', 'lightVertical',
# 'mediumGray', 'darkTrellis', 'lightGray', 'darkUp', 'darkGrid', 'lightHorizontal', 'lightUp', 'darkDown',
# 'darkGray', 'darkVertical', 'lightDown'
from openpyxl.styles.fills import PatternFill
cell.fill = PatternFill(patternType='solid', fgColor=Color(indexed=10))
protection模块
openpyxl.styles.protection模块
设置单元格保护
对应Excel设置:
from openpyxl.styles.protection import Protection
protec = Protection(locked=False, hidden=True)
cell.protection = protec
colors模块
openpyxl.styles.colors模块 用于定义颜色设置
颜色可以通过三种方式设置:索引、aRGB或主题
from openpyxl.styles.colors import Color
color = "FF0000"
color = Color(indexed=32)
color = Color(theme=6, tint=0.5)
索引对应的颜色:
以上就是对openpyxl库添加样式设置的一些学习!
如果有什么不对的地方,欢迎指正!
参考: openpyxl.styles包