python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python openpyxl格式化Excel文件

使用Python的openpyxl库批量格式化Excel文件的方法

作者:小庄-Python办公

本文介绍了使用Python的openpyxl库批量格式化Excel文件的方法,通过定义标题行样式(蓝色背景、加粗字体)、数据行样式(细边框、自动对齐)和自动调整列宽等操作,实现报表格式的自动化统一,文章提供了完整的代码实现,需要的朋友可以参考下

场景引入

公司规定所有报表必须使用统一的格式:标题行加粗、背景色蓝色、字号 12、所有单元格加边框。但每次从系统导出的数据都是"裸表"——没有任何格式。公司有 50 份这样的报表需要格式化,手动调整每份表格至少要 5 分钟,总计 4 个小时。

本节教你用 Python + openpyxl 实现 一键格式化,50 份表格只需 10 秒。

技术原理

pandas 擅长数据处理,但不擅长格式控制。本节引入 openpyxl 库,它可以直接操作 Excel 的格式属性:

格式属性openpyxl 对应
字体Font(name, size, bold, italic, color)
填充(背景色)PatternFill(start_color, fill_type)
边框Border(left, right, top, bottom)
对齐Alignment(horizontal, vertical)
列宽worksheet.column_dimensions[col].width

工作流程:

裸数据.xlsx → openpyxl 加载 → 应用格式规则 → 保存格式化后的文件

环境准备

pip install openpyxl

完整代码

import os
from openpyxl import load_workbook
from openpyxl.styles import Font, PatternFill, Border, Side, Alignment

def format_excel_sheet(input_file, output_file=None):
    """
    为 Excel 文件添加统一的格式:标题行加粗、蓝色背景、边框、对齐

    参数:
        input_file: 输入的原始 Excel 文件
        output_file: 输出的格式化文件(默认在原文件名后加_已格式化)
    """
    if output_file is None:
        base_name = os.path.splitext(input_file)[0]
        output_file = f"{base_name}_已格式化.xlsx"

    # 1. 加载工作簿
    wb = load_workbook(input_file)
    ws = wb.active

    # 2. 定义样式对象
    # 标题行字体:微软雅黑,12号,加粗,白色
    title_font = Font(name='微软雅黑', size=12, bold=True, color='FFFFFF')

    # 标题行背景:蓝色
    title_fill = PatternFill(start_color='4472C4', fill_type='solid')

    # 数据行字体:微软雅黑,11号
    data_font = Font(name='微软雅黑', size=11)

    # 细边框
    thin_border = Border(
        left=Side(style='thin'),
        right=Side(style='thin'),
        top=Side(style='thin'),
        bottom=Side(style='thin')
    )

    # 居中对齐
    center_align = Alignment(horizontal='center', vertical='center')

    # 左对齐(适合文本列)
    left_align = Alignment(horizontal='left', vertical='center')

    # 3. 获取数据范围
    max_row = ws.max_row
    max_col = ws.max_column

    if max_row < 1:
        print(f"警告: {input_file} 没有数据")
        return

    # 4. 格式化标题行(第一行)
    for col in range(1, max_col + 1):
        cell = ws.cell(row=1, column=col)
        cell.font = title_font
        cell.fill = title_fill
        cell.border = thin_border
        cell.alignment = center_align

    # 5. 格式化数据行(第二行到最后)
    for row in range(2, max_row + 1):
        for col in range(1, max_col + 1):
            cell = ws.cell(row=row, column=col)
            cell.font = data_font
            cell.border = thin_border
            # 文本列左对齐,数字列居中
            if isinstance(cell.value, (int, float)):
                cell.alignment = center_align
            else:
                cell.alignment = left_align

    # 6. 自动调整列宽
    for col in range(1, max_col + 1):
        max_length = 0
        column_letter = ws.cell(row=1, column=col).column_letter

        for row in range(1, max_row + 1):
            cell_value = ws.cell(row=row, column=col).value
            if cell_value:
                cell_length = len(str(cell_value))
                if cell_length > max_length:
                    max_length = cell_length

        # 设置列宽(最大 30,最小 10)
        adjusted_width = min(max(max_length + 2, 10), 30)
        ws.column_dimensions[column_letter].width = adjusted_width

    # 7. 冻结首行(滚动时标题行固定不动)
    ws.freeze_panes = 'A2'

    # 8. 保存
    wb.save(output_file)
    print(f"格式化完成: {output_file}")


# ==================== 批量格式化 ====================
def batch_format_excel_files(folder_path):
    """
    批量格式化文件夹中的所有 Excel 文件
    """
    count = 0
    for filename in os.listdir(folder_path):
        if filename.endswith('.xlsx') and '已格式化' not in filename:
            file_path = os.path.join(folder_path, filename)
            format_excel_sheet(file_path)
            count += 1
    print(f"\n批量格式化完成!共处理 {count} 个文件")


# ==================== 使用示例 ====================
if __name__ == "__main__":
    # 单个文件格式化
    # format_excel_sheet("原始数据.xlsx")

    # 批量格式化文件夹
    batch_format_excel_files(r"D:\待格式化报表")

代码逐行解析

1. 样式对象定义

openpyxl 中的样式是可复用的对象:

title_font = Font(name='微软雅黑', size=12, bold=True, color='FFFFFF')

Font 参数说明:

参数说明示例值
name字体名称‘微软雅黑’, ‘Arial’, ‘宋体’
size字号11, 12, 14
bold是否加粗True / False
italic是否斜体True / False
color字体颜色(HEX 六位码)‘FFFFFF’(白色), ‘FF0000’(红色)

2. 背景色填充

title_fill = PatternFill(start_color='4472C4', fill_type='solid')

常用颜色对照:

3. 边框设置

thin_border = Border(
    left=Side(style='thin'),
    right=Side(style='thin'),
    top=Side(style='thin'),
    bottom=Side(style='thin')
)

Sidestyle 可选值:'thin', 'medium', 'thick', 'double', 'dashed', 'dotted'

4. 自动列宽计算

adjusted_width = min(max(max_length + 2, 10), 30)

这段代码的逻辑:

5. 冻结首行

ws.freeze_panes = 'A2'

冻结 A2 单元格上方的行(即第一行),滚动数据时标题行始终可见。

进阶技巧

技巧 1:条件格式——自动高亮异常值

from openpyxl.formatting.rule import CellIsRule

# 高亮负数(红色背景)
ws.conditional_formatting.add(
    f'B2:B{max_row}',
    CellIsRule(
        operator='lessThan',
        formula=['0'],
        fill=PatternFill(start_color='FFC7CE', fill_type='solid')
    )
)

技巧 2:隔行变色(斑马纹)

light_fill = PatternFill(start_color='F2F2F2', fill_type='solid')

for row in range(2, max_row + 1):
    if row % 2 == 0:  # 偶数行
        for col in range(1, max_col + 1):
            ws.cell(row=row, column=col).fill = light_fill

技巧 3:从模板复制格式

如果公司已有标准模板,可以先复制模板再写入数据:

import shutil

shutil.copy("公司标准模板.xlsx", output_file)
wb = load_workbook(output_file)
ws = wb.active

# 此时已有模板的所有格式,只需写入数据即可
for row_idx, row_data in enumerate(data_rows, start=2):
    for col_idx, value in enumerate(row_data, start=1):
        ws.cell(row=row_idx, column=col_idx, value=value)

常见问题

Q1:保存时报错ValueError: Max value is 255

原因:openpyxl 对旧版 .xls 格式支持有限。

解决:确保文件是 .xlsx 格式。如果是 .xls,先用 Excel 另存为 .xlsx

Q2:中文显示为方框(乱码)?

原因:系统缺少对应字体。

解决:改用通用字体:

title_font = Font(name='Arial Unicode MS', size=12, bold=True)
# 或
title_font = Font(name='SimSun', size=12, bold=True)  # 宋体

Q3:如何设置行高?

# 设置第一行行高为 25
ws.row_dimensions[1].height = 25

# 批量设置所有数据行行高
for row in range(1, max_row + 1):
    ws.row_dimensions[row].height = 20

总结

格式需求openpyxl 类关键参数
字体Font()name, size, bold, color
背景色PatternFill()start_color, fill_type
边框Border() + Side()style=‘thin’/‘medium’
对齐Alignment()horizontal, vertical
列宽column_dimensions[].width数值
行高row_dimensions[].height数值
冻结窗格freeze_panes单元格地址如 ‘A2’

本节掌握了用 Python 自动控制 Excel 格式的能力。配合前两节的合并和拆分,你已经可以处理大部分 Excel 批量操作场景。

以上就是使用Python的openpyxl库批量格式化Excel文件的方法的详细内容,更多关于Python openpyxl格式化Excel文件的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文