openpyxl 学习笔记

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
    #print(file_path) 文件路径
    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)   		#保存当前工作簿




















在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值