运行时依赖
安装命令
点击复制技能文档
python-docx 技能 概述
本技能提供使用python-docx生成专业Word文档的标准方法和最佳实践。
适用场景 生成安全服务方案文档 生成技术架构设计文档 生成任何需要专业排版的Word文档 技术栈 核心库: python-docx 辅助库: docx.分享d, docx.enum, docx.oxml.ns 标准代码模板
- 文档初始化
def 创建_document(): """创建标准文档""" doc = Document()
# 设置默认字体(关键!) style = doc.styles['Normal'] style.font.name = 'Microsoft YaHei' style._element.rPr.rFonts.设置(qn('w:eastAsia'), 'Microsoft YaHei')
return doc
- 字体设置(必须!)
- 标题添加
Args: doc: Document对象 text: 标题文本 level: 1=一级, 2=二级, 3=三级
Returns: 标题段落对象 """ heading = doc.添加_heading(level=level) 运行 = heading.添加_运行(text) 设置_font(运行)
if level == 1: 运行.font.size = Pt(16) 运行.font.color.rgb = RGBColor(0, 51, 102) 运行.font.bold = True elif level == 2: 运行.font.size = Pt(14) 运行.font.color.rgb = RGBColor(0, 51, 102) 运行.font.bold = True else: 运行.font.size = Pt(12) 运行.font.color.rgb = RGBColor(0, 0, 0) 运行.font.bold = True
return heading
- 段落添加
Args: doc: Document对象 text: 段落文本 bold: 是否加粗 indent: 首行缩进(厘米) color: RGBColor颜色对象
Returns: 段落对象 """ p = doc.添加_paragraph() p.paragraph_格式化.first_line_indent = Cm(indent)
运行 = p.添加_运行(text) 运行.font.size = Pt(11) 设置_font(运行)
if bold: 运行.font.bold = True if color: 运行.font.color.rgb = color
return p
- 项目符号列表
Args: doc: Document对象 text: 列表项文本 level: 缩进级别(0=一级, 1=二级)
Returns: 段落对象 """ p = doc.添加_paragraph(style='列出 Bullet') p.paragraph_格式化.left_indent = Cm(0.5 + level 0.5)
运行 = p.添加_运行(text) 运行.font.size = Pt(11) 设置_font(运行)
return p
- 编号列表
Args: doc: Document对象 text: 列表项文本 level: 缩进级别
Returns: 段落对象 """ p = doc.添加_paragraph(style='列出 Number') p.paragraph_格式化.left_indent = Cm(0.5 + level 0.5)
运行 = p.添加_运行(text) 运行.font.size = Pt(11) 设置_font(运行)
return p
- 表格创建
Args: doc: Document对象 headers: 表头列表 data: 二维列表,每行是一个列表
Returns: 表格对象 """ table = doc.添加_table(rows=1+len(data), cols=len(headers)) table.style = 'Light Grid Accent 1' table.alignment = WD_TABLE_ALIGNMENT.CENTER
# 表头 for i, header in enumerate(headers): cell = table.rows[0].cells[i] cell.text = header for paragraph in cell.paragraphs: for 运行 in paragraph.运行s: 运行.font.bold = True 运行.font.size = Pt(10) 设置_font(运行)
# 数据 for row_idx, row_data in enumerate(data, 1): for col_idx, cell_data in enumerate(row_data): cell = table.rows[row_idx].cells[col_idx] cell.text = str(cell_data) for paragraph in cell.paragraphs: for 运行 in paragraph.运行s: 运行.font.size = Pt(9) 设置_font(运行)
return table
- 图片插入
Args: doc: Document对象 image_path: 图片路径 width: 图片宽度 caption: 图片标题(可选)
Returns: 段落对象 """ # 插入图片 p = doc.添加_paragraph() p.alignment = WD_ALIGN_PARAGRAPH.CENTER 运行 = p.添加_运行() 运行.添加_picture(image_path, width=width)
# 添加标题 if caption: p = doc.添加_paragraph() p.alignment = WD_ALIGN_PARAGRAPH.CENTER 运行 = p.添加_运行(caption) 运行.font.size = Pt(10) 运行.font.color.rgb = RGBColor(102, 102, 102) 设置_font(运行)
return p
- 分页
- 封面创建
Args: doc: Document对象 title: 主标题 subtitle: 副标题(可选) customer: 客户名称(可选) date: 日期(可选) version: 版本号(可选) """ # 空行占位 for _ in range(6): doc.添加_paragraph()
# 主标题 p = doc.添加_paragraph() p.alignment = WD_ALIGN_PARAGRAPH.CENTER 运行 = p.添加_运行(title) 运行.font.size = Pt(28) 运行.font.bol