Excel需求文档智能转换Word工具
编辑:梨涡 分类:python 日期:2025-03-04 09:22:04 访问量:78

 1. 核心功能

  • 智能字段映射:用户从Excel表头选择需导出的字段(如“需求编号”“功能描述”),程序精准提取对应数据,避免冗余信息干扰。
  • 动态标题生成:支持自定义标题模板(如{模块}_{功能名称}),通过占位符动态拼接Excel字段值,生成层次分明的多级标题(如“用户管理_权限配置”)。
  • 表格格式化输出:自动创建两列式Word表格(字段名+内容),固定列宽(3cm+17cm),统一宋体10号字,表头居中显示,并可扩展灰色背景高亮。
  • 样式灵活可控:提供标题级别设置(如三级标题)、表格边框粗细调整,适配企业文档规范。

2. 技术亮点

  • 双库协同:利用openpyxl解析Excel数据,python-docx操作Word元素,实现跨格式无缝转换。
  • 正则解析模板:通过正则表达式解析{字段名}占位符,动态替换为实际数据,提升标题灵活性。
  • 样式批处理:批量设置字体、对齐方式及表格边框,确保文档风格一致性。

3. 适用场景

  • 产品需求管理:将Excel中的功能需求清单快速转为技术文档,便于团队评审与归档。
  • 项目报告生成:自动化输出测试用例、配置清单等标准化表格,减少手动复制粘贴。
  • 企业模板化办公:通过预配置字段与标题模板,一键生成符合内部规范的交付物。

4. 使用价值

  • 效率提升:免除手动整理数据、调整格式的时间成本,复杂文档生成仅需数秒。
  • 错误规避:数据直接来自Excel源文件,避免人为转录错误。
  • 标准化输出:确保文档结构、样式符合企业或行业规范,提升专业度。

备注:程序已内置示例路径与字段配置,用户仅需修改Excel/Word文件路径及字段选择即可快速适配实际项目。

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project :PythonProject
@File    :excel_to_docx.py
@IDE     :PyCharm
@Author  :Spring Zhang
@Date    :2025-01-21 11:51
@Note    :
'''
import os.path
import openpyxl
from docx import Document
from docx.shared import Pt, Cm
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml.ns import nsdecls
from docx.oxml import parse_xml
import re


def parse_title_field(title_field, header_index, sheet, row_index):
    """
    解析 title_field 并生成标题内容
    :param title_field: 标题字段模板,如 "{模块}_{功能名称}"
    :param header_index: 表头字段及其索引的字典
    :param sheet: Excel 工作表对象
    :param row_index: 当前行索引
    :return: 生成的标题内容
    """
    title_content_parts = []
    field_pattern = re.compile(r'{(\w+)}')
    for match in field_pattern.finditer(title_field):
        field_name = match.group(1)
        if field_name in header_index:
            col = header_index[field_name]
            field_value = sheet.cell(row_index, col).value
            if field_value is None:
                field_value = ""
            title_content_parts.append(field_value)
        else:
            title_content_parts.append(field_name)
    return re.sub(field_pattern, lambda x: str(title_content_parts.pop(0)), title_field)


def add_table(document, selected_fields, header_index, sheet, row_index):
    """
    向文档中添加表格
    :param document: Word 文档对象
    :param selected_fields: 需要显示的字段列表
    :param header_index: 表头字段及其索引的字典
    :param sheet: Excel 工作表对象
    :param row_index: 当前行索引
    """
    table = document.add_table(rows=0, cols=2)
    table.columns[0].width = Cm(3)
    table.columns[1].width = Cm(17)
    table.style = 'Table Grid'
    for field in selected_fields:
        if field in header_index:
            col = header_index[field]
            new_row = table.add_row()
            new_row.cells[0].text = field
            excel_value = sheet.cell(row_index, col).value
            if excel_value is None:
                excel_value = ""
            new_row.cells[1].text = excel_value
            for cell in new_row.cells:
                for paragraph in cell.paragraphs:
                    for run in paragraph.runs:
                        run.font.name = '宋体'
                        run.font.size = Pt(10)
                        run.font.bold = False
            first_col_paragraph = new_row.cells[0].paragraphs[0]
            first_col_paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER


def read_excel_and_write_word(excel_file_path, word_file_path, selected_fields, title_level, title_field):
    """
    读取 Excel 文件并将数据写入 Word 文件
    :param excel_file_path: Excel 文件路径
    :param word_file_path: Word 文件路径
    :param selected_fields: 需要显示的字段列表
    :param title_level: 标题样式级别
    :param title_field: 标题字段模板
    """
    workbook = openpyxl.load_workbook(excel_file_path)
    sheet = workbook.active
    document = Document()
    num_rows = sheet.max_row
    num_cols = sheet.max_column
    header_index = {sheet.cell(1, col).value: col for col in range(1, num_cols + 1) if sheet.cell(1, col).value in selected_fields}

    for i in range(2, num_rows + 1):
        title_content = parse_title_field(title_field, header_index, sheet, i)
        document.add_heading(title_content, level=title_level)
        add_table(document, selected_fields, header_index, sheet, i)
        # document.add_paragraph("\n\n")  #表格结尾空2行

    document.save(word_file_path)
    print("完成")


def set_header_background(cell):
    """
    为表格表头添加灰色背景
    :param cell: 要添加背景的单元格
    """
    tcPr = cell._tc.get_or_add_tcPr()
    shd = parse_xml('<w:shd xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:fill="D9D9D9"/>')
    tcPr.append(shd)


def set_border(table, border_size):
    """
    为表格添加边框
    :param table: 要添加边框的表格
    :param border_size: 边框的大小
    """
    tbl = table._tbl
    tblPr = tbl.get_or_add_tblPr()
    for border in ('top', 'bottom', 'left', 'right'):
        tcBorder = parse_xml(f'<w:tcBorders xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:{border} w:val="single" w:sz="{border_size}" w:space="0" w:color="000000"/></w:tcBorders>')
        tblPr.append(tcBorder)


if __name__ == '__main__':
    path = r"D:\WorkFiles\产品中心\1 医疗系列\1 静态医疗(DR) RIASDR\标准版\奕安\需求&原型"
    xls = "20250122 PAD功能需求清单 v1.1.xlsx"
    docx = "20250122 PAD功能需求清单 v1.1.docx"
    selected_fields = ["需求编号", "模块", "功能名称", "功能描述", "前置条件", "输入", "处理", "输出", "版本"]    #设置导出word表格的显示字段,要导出哪些字段就加进来
    title_level = 3     # 自定义标题样式级别
    # title_field = "{模块}_{功能名称}" # 自由组合标题,从selected_fields里面选组合或者自己加前后缀
    title_field = "{功能名称}" # 自由组合标题
    read_excel_and_write_word(os.path.join(path, xls), os.path.join(path, docx), selected_fields, title_level, title_field)

    print(f"正在打开文件夹:{path}")
    os.startfile(path)

 

返回顶部