openpyxl 学习笔记
from openpyxl import load_workbook, workbook
from openpyxl.styles import PatternFill, Side, Border, Alignment
import os
path = '../example/各部门利润表汇总_副本/'
files = os.listdir('../example/各部门利润表汇总_副本/')
head_pattern = PatternFill(fill_type='darkVertical', fgColor='FF7F24')
body_pattern = PatternFill(fill_type='darkVertical', fgColor='FFFFE0')
rear_pattern = PatternFill(fill_type='darkVertical', fgColor='EE9572')
alig = Alignment(horizontal='center', vertical='top')
side = Side('thin')
border = Border(bottom=side)
for sample_file in files:
file_path = path + sample_file
wb = load_workbook(file_path)
ws = wb.active
bottom_row = ws.max_row
for cell in ws[1]:
cell.fill = head_pattern
cell.alignment = alig
cell.border = border
for cell in ws[bottom_row]:
cell.fill = rear_pattern
cell.alignment = alig
cell.border = border
for row in ws.iter_rows(min_row=2, max_row=bottom_row-1):
for cell in row :
cell.fill = body_pattern
cell.alignment = alig
cell.border = border
wb.save(file_path)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/09c41348ecec0ff5c70af8b30a00453b.png)