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)
'single',
'double',
'singleAccounting',
'doubleAccounting'
"major", (标题)
"minor" (正文)
'superscript', 上标
'subscript', 下标
'baseline' 普通
import openpyxl
from openpyxl.styles import Font
wb = openpyxl.Workbook()
ws = wb.active
ws.append(['name','number','unit','price'])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
# 设置标题
for c in range(1,5):
ws.cell(1,c).font = Font(name='宋体',size=14,bold=True)
# 设置正文内容
for r in range(2,7):
for c in range(1,5):
ws.cell(r,c).font = Font(name='宋体',size=12)
wb.save(r'/Users/junliangchen/Desktop/test.xlsx')
Side(style=None, color=None, border_style=None)
'dashDot','dashDotDot', 'dashed','dotted',
'double','hair', 'medium', 'mediumDashDot',
'mediumDashDotDot', 'mediumDashed', 'slantDashDot', 'thick', 'thin'
Border(left=None, right=None, top=None, bottom=None, diagonal=None, diagonal_direction=None, vertical=None, horizontal=None, diagonalUp=False, diagonalDown=False, outline=True, start=None, end=None)
import openpyxl
from openpyxl.styles import Font, Border, Side
wb = openpyxl.Workbook()
ws = wb.active
ws.append(['name','number','unit','price'])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
# 设置标题
for c in range(1,5):
ws.cell(1,c).font = Font(name='宋体',size=14,bold=True)
# 设置正文内容
for r in range(2,7):
for c in range(1,5):
ws.cell(r,c).font = Font(name='宋体',size=12)
# 设置边框
for r in range(1,7):
for c in range(1,5):
ws.cell(r,c).border = Border(top=Side(style='thin',color='1E1E1E'),
right=Side(style='thin',color='1E1E1E'),
bottom=Side(style='thin',color='1E1E1E'),
left=Side(style='thin',color='1E1E1E'),diagonalUp=True,diagonalDown=True,diagonal=Side(style='thin',color='1E1E1E'))
wb.save(r'/Users/junliangchen/Desktop/test.xlsx')
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)
"general", 常规
"left", 靠左
"center",居中
"right",靠右
"fill", 填充
"justify", 两端对齐
"centerContinuous",跨列居中
"distributed" 分散对齐
"top", "center", "bottom",
"justify", 两端对齐
"distributed",分散对齐
import openpyxl
from openpyxl.styles import Font, Border, Side, Alignment
wb = openpyxl.Workbook()
ws = wb.active
ws.append(['name','number','unit','price'])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
# 设置标题
for c in range(1,5):
ws.cell(1,c).font = Font(name='宋体',size=14,bold=True)
# 设置正文内容
for r in range(2,7):
for c in range(1,5):
ws.cell(r,c).font = Font(name='宋体',size=12)
# 设置边框
for r in range(1,7):
for c in range(1,5):
ws.cell(r,c).border = Border(top=Side(style='thin',color='1E1E1E'),
right=Side(style='thin',color='1E1E1E'),
bottom=Side(style='thin',color='1E1E1E'),
left=Side(style='thin',color='1E1E1E'),diagonalUp=True,diagonalDown=True,diagonal=Side(style='thin',color='1E1E1E'))
# 设置对齐方式
ws.cell(r,c).alignment = Alignment(horizontal='center',vertical='center')
wb.save(r'/Users/junliangchen/Desktop/test.xlsx')
PatternFill(patternType=None, fgColor=Color(), bgColor=Color(), fill_type=None, start_color=None, end_color=None)
FILL_NONE = 'none'
FILL_SOLID = 'solid'
FILL_PATTERN_DARKDOWN = 'darkDown'
FILL_PATTERN_DARKGRAY = 'darkGray'
FILL_PATTERN_DARKGRID = 'darkGrid'
FILL_PATTERN_DARKHORIZONTAL = 'darkHorizontal'
FILL_PATTERN_DARKTRELLIS = 'darkTrellis'
FILL_PATTERN_DARKUP = 'darkUp'
FILL_PATTERN_DARKVERTICAL = 'darkVertical'
FILL_PATTERN_GRAY0625 = 'gray0625'
FILL_PATTERN_GRAY125 = 'gray125'
FILL_PATTERN_LIGHTDOWN = 'lightDown'
FILL_PATTERN_LIGHTGRAY = 'lightGray'
FILL_PATTERN_LIGHTGRID = 'lightGrid'
FILL_PATTERN_LIGHTHORIZONTAL = 'lightHorizontal'
FILL_PATTERN_LIGHTTRELLIS = 'lightTrellis'
FILL_PATTERN_LIGHTUP = 'lightUp'
FILL_PATTERN_LIGHTVERTICAL = 'lightVertical'
FILL_PATTERN_MEDIUMGRAY = 'mediumGray'
import openpyxl
from openpyxl.styles import Font, Border, Side, Alignment, PatternFill
wb = openpyxl.Workbook()
ws = wb.active
ws.append(['name','number','unit','price'])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
# 设置标题
for c in range(1,5):
ws.cell(1,c).font = Font(name='宋体',size=14,bold=True)
# 设置填充色
ws.cell(1,c).fill = PatternFill(fill_type='solid',start_color='FB536C') # 红色
# 设置正文内容
for r in range(2,7):
for c in range(1,5):
ws.cell(r,c).font = Font(name='宋体',size=12)
# 设置边框
for r in range(1,7):
for c in range(1,5):
ws.cell(r,c).border = Border(top=Side(style='thin',color='1E1E1E'),
right=Side(style='thin',color='1E1E1E'),
bottom=Side(style='thin',color='1E1E1E'),
left=Side(style='thin',color='1E1E1E'),diagonalUp=True,diagonalDown=True,diagonal=Side(style='thin',color='1E1E1E'))
ws.cell(r,c).alignment = Alignment(horizontal='center',vertical='center')
wb.save(r'/Users/junliangchen/Desktop/test.xlsx')
worksheet.number_format = 'General'
0: 'General',
1: '0',
2: '0.00',
3: '#,##0',
4: '#,##0.00',
5: '"$"#,##0_);("$"#,##0)',
6: '"$"#,##0_);[Red]("$"#,##0)',
7: '"$"#,##0.00_);("$"#,##0.00)',
8: '"$"#,##0.00_);[Red]("$"#,##0.00)',
9: '0%',
10: '0.00%',
11: '0.00E+00',
12: '# ?/?',
13: '# ??/??',
14: 'mm-dd-yy',
15: 'd-mmm-yy',
16: 'd-mmm',
17: 'mmm-yy',
18: 'h:mm AM/PM',
19: 'h:mm:ss AM/PM',
20: 'h:mm',
21: 'h:mm:ss',
22: 'm/d/yy h:mm',
37: '#,##0_);(#,##0)',
38: '#,##0_);[Red](#,##0)',
39: '#,##0.00_);(#,##0.00)',
40: '#,##0.00_);[Red](#,##0.00)',
41: r'_(* #,##0_);_(* \(#,##0\);_(* "-"_);_(@_)',
42: r'_("$"* #,##0_);_("$"* \(#,##0\);_("$"* "-"_);_(@_)',
43: r'_(* #,##0.00_);_(* \(#,##0.00\);_(* "-"??_);_(@_)',
44: r'_("$"* #,##0.00_)_("$"* \(#,##0.00\)_("$"* "-"??_)_(@_)',
45: 'mm:ss',
46: '[h]:mm:ss',
47: 'mmss.0',
48: '##0.0E+0',
49: '@', }
from openpyxl.comments import Comment
Comment(text, author, height=79, width=144)
import openpyxl
from openpyxl.styles import Font, Border, Side, Alignment, PatternFill
from openpyxl.comments import Comment
wb = openpyxl.Workbook()
ws = wb.active
ws.append(['name','number','unit','price'])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
# 设置标题
for c in range(1,5):
ws.cell(1,c).font = Font(name='宋体',size=14,bold=True)
# 设置填充色
ws.cell(1,c).fill = PatternFill(fill_type='solid',start_color='FB536C') # 红色
# 设置正文内容
for r in range(2,7):
for c in range(1,5):
ws.cell(r,c).font = Font(name='宋体',size=12)
# 设置边框
for r in range(1,7):
for c in range(1,5):
ws.cell(r,c).border = Border(top=Side(style='thin',color='1E1E1E'),
right=Side(style='thin',color='1E1E1E'),
bottom=Side(style='thin',color='1E1E1E'),
left=Side(style='thin',color='1E1E1E'),diagonalUp=True,diagonalDown=True,diagonal=Side(style='thin',color='1E1E1E'))
ws.cell(r,c).alignment = Alignment(horizontal='center',vertical='center')
# 设置数字格式
for r in range(2,7):
ws.cell(r,3).number_format = '#,##0.00'
# 设置注释
ws.cell(1,1).comment = Comment(text='增加注释',author='Jc')
wb.save(r'/Users/junliangchen/Desktop/test.xlsx')
Protection(locked=True, hidden=False)
import openpyxl
from openpyxl.styles import Font, Border, Side, Alignment, PatternFill, Protection
from openpyxl.comments import Comment
wb = openpyxl.Workbook()
ws = wb.active
ws.append(['name','number','unit','price'])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
ws.append(['A',2,5,0])
# 设置标题
for c in range(1,5):
ws.cell(1,c).font = Font(name='宋体',size=14,bold=True)
# 设置填充色
ws.cell(1,c).fill = PatternFill(fill_type='solid',start_color='FB536C') # 红色
# 设置正文内容
for r in range(2,7):
for c in range(1,5):
ws.cell(r,c).font = Font(name='宋体',size=12)
# 设置边框
for r in range(1,7):
for c in range(1,5):
ws.cell(r,c).border = Border(top=Side(style='thin',color='1E1E1E'),
right=Side(style='thin',color='1E1E1E'),
bottom=Side(style='thin',color='1E1E1E'),
left=Side(style='thin',color='1E1E1E'),diagonalUp=True,diagonalDown=True,diagonal=Side(style='thin',color='1E1E1E'))
ws.cell(r,c).alignment = Alignment(horizontal='center',vertical='center')
# 设置数字格式
for r in range(2,7):
ws.cell(r,3).number_format = '#,##0.00'
# 设置注释
ws.cell(1,1).comment = Comment(text='增加注释',author='Jc')
# 保护
ws.cell(1,1).protection = Protection(locked=False,
hidden=False)
wb.save(r'/Users/junliangchen/Desktop/test.xlsx')