{模块}_{功能名称}
),通过占位符动态拼接Excel字段值,生成层次分明的多级标题(如“用户管理_权限配置”)。openpyxl
解析Excel数据,python-docx
操作Word元素,实现跨格式无缝转换。{字段名}
占位符,动态替换为实际数据,提升标题灵活性。备注:程序已内置示例路径与字段配置,用户仅需修改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)