ITPub博客

首页 > 应用开发 > Javascript > 同一文档中的页眉在各页面间基本保持一致

同一文档中的页眉在各页面间基本保持一致

投稿 Javascript 作者:crucz025 时间:2026-01-28 18:43:48 0 删除 编辑

创建新文档 doc = Document ( ) # 获取文档的第一个节(新建文档默认只有1个节) section1 = doc . sections [ 0 ] header1 = section1 . header # 检查初始状态:新建文档的页眉默认关联到前一节页眉设置(此处无前置节,故无实际页眉) print ( f"初始页眉关联状态: {header1 .is_linked_to_previous } " )   # 输出: True # 为第一节添加简单页眉(仅左侧文本) # 直接编辑页眉的第一个段落(新建页眉默认包含1个空段落) para1 = header1 . paragraphs [ 0 ] para1 . text = "文档主标题 - 第一节页眉" # 自动创建独立页眉定义,关联状态变为False print ( f"添加页眉后关联状态: {header1 .is_linked_to_previous } " )   # 输出: False # -------------------------- 2. 添加带分区(左/中/右)的页眉 -------------------------- # 先清空第一节原有页眉内容,重新设置分区页眉 para1 . clear ( ) # 使用制表符\t分隔左、中、右三部分内容 para1 . text = "左侧:文档名称\t居中:2026年1月\t右侧:页码" # 应用Word默认的"Header"样式(确保制表位生效) para1 . style = doc . styles [ "Header" ] # -------------------------- 3. 添加新节并设置独立页眉 -------------------------- # 向文档末尾添加新节(section2) section2 = doc . add_section ( ) header2 = section2 . header # 初始状态:新节的页眉默认关联到前一节(section1) print ( f"第二节初始关联状态: {header2 .is_linked_to_previous } " )   # 输出: True # 验证:此时编辑header2的内容会直接修改section1的页眉(继承特性) # 先取消关联,创建第二节的独立页眉 header2 . is_linked_to_previous = False # 为第二节添加专属页眉 para2 = header2 . paragraphs [ 0 ] para2 . text = "第二节独立页眉 - 仅本节显示" # -------------------------- 4. 添加页脚(与页眉用法完全一致) -------------------------- # 为第一节添加页脚(包含页码) footer1 = doc . sections [ 0 ] . footer footer1 . is_linked_to_previous = False   # 取消关联,创建独立页脚 footer_para1 = footer1 . paragraphs [ 0 ] footer_para1 . text = "\t第 {PAGE} 页 / 共 {NUMPAGES} 页\t"   # 居中显示页码 footer_para1 . style = doc . styles [ "Footer" ] # 为第二节添加不同的页脚 footer2 = doc . sections [ 1 ] . footer footer2 . is_linked_to_previous = False footer_para2 = footer2 . paragraphs [ 0 ] footer_para2 . text = "第二节页脚 - 联系人:测试账号" # -------------------------- 5. 删除页眉(恢复关联状态) -------------------------- # 若需删除第二节的独立页眉,只需将其关联状态设为True(会删除内容) # 此处仅演示,注释掉避免影响最终效果


from docx . enum . style import WD_STYLE_TYPE from docx . shared import Pt , Inches from docx . enum . text import WD_UNDERLINE # 1. 创建新文档并访问样式集合 doc = Document ( ) styles = doc . styles print ( f"初始样式数量: { len (styles ) } " ) # 2. 访问并筛选特定类型的样式 # 筛选所有段落样式并打印名称 print ( "\n=== 所有段落样式 ===" ) paragraph_styles = [ s for s in styles if s . type == WD_STYLE_TYPE . PARAGRAPH ] for style in paragraph_styles [ : 5 ] :   # 只打印前5个避免输出过长     print ( f"- {style .name } " ) # 3. 应用现有样式到新段落 # 方式1: 创建时指定样式名称 doc . add_heading ( "样式操作示例" , level = 0 ) heading1_para = doc . add_paragraph ( "这是应用 Heading 1 样式的段落" , style = "Heading 1" ) # 方式2: 先创建段落再赋值样式对象 normal_para = doc . add_paragraph ( ) normal_para . text = "这是先创建再应用 Normal 样式的段落" normal_para . style = styles [ "Normal" ] # 4. 创建自定义样式 # 4.1 创建段落样式并设置基础样式、字符格式、段落格式 citation_style = styles . add_style ( "Citation" , WD_STYLE_TYPE . PARAGRAPH ) # 设置基础样式(继承 Normal 样式的格式) citation_style . base_style = styles [ "Normal" ] # 设置字符格式 citation_style . font . name = "宋体" citation_style . font . size = Pt ( 10 ) citation_style . font . italic = True   # 斜体 citation_style . font . underline = WD_UNDERLINE . DOT_DASH   # 点划线下划线 # 设置段落格式(悬挂缩进、段前间距) citation_style . paragraph_format . left_indent = Inches ( 0.25 ) citation_style . paragraph_format . first_line_indent = Inches ( - 0.25 ) citation_style . paragraph_format . space_before = Pt ( 6 ) # 设置后续段落样式 citation_style . next_paragraph_style = styles [ "Normal" ] # 应用自定义样式 citation_para = doc . add_paragraph ( "这是自定义 Citation 样式的引用文本" , style = "Citation" ) print ( f"\n自定义样式应用后名称: {citation_para .style .name } " ) # 5. 控制样式在 Word 中的显示(快速样式、优先级) body_text_style = styles [ "Body Text" ] body_text_style . hidden = False body_text_style . quick_style = True body_text_style . priority = 1 # 6. 处理潜在样式(Latent Styles) latent_styles = styles . latent_styles print ( f"\n潜在样式总数: { len (latent_styles ) } " ) # 为 List Bullet 添加潜在样式定义 if "List Bullet" not in latent_styles :    latent_list_bullet = latent_styles . add_latent_style ( "List Bullet" )    latent_list_bullet . hidden = False    latent_list_bullet . priority = 2    latent_list_bullet . quick_style = True print ( f"List Bullet 潜在样式优先级: {latent_styles [ 'List Bullet' ] .priority } " ) # 7. 删除样式 print ( f"\n删除前样式数量: { len (styles ) } " ) if "Citation" in styles :    styles [ "Citation" ] . delete ( )     print ( f"删除后样式数量: { len (styles ) } " ) # 8. 保存文档


创建文档并演示批注的完整操作流程: 1. 创建基础批注 2. 创建富文本格式批注 3. 修改批注元数据 4. 访问和遍历所有批注 """ # 1. 初始化文档对象 doc = Document ( ) doc . add_heading ( "Python-docx 批注演示文档" , level = 1 ) # 2. 示例1:创建基础文本批注 para1 = doc . add_paragraph ( "这是第一段测试文本,用于添加基础批注。" ) # 为整段文本添加基础批注 basic_comment = doc . add_comment (    runs = para1 . runs ,   # 关联的文本块    text = "基础批注:这段文本表述清晰,建议保留。" ,   # 批注纯文本内容    author = "张三" ,   # 批注作者    initials = "ZS"   # 作者缩写 ) print ( f"✅ 基础批注创建完成" ) # 3. 示例2:创建富文本格式批注(包含粗体、斜体等样式) para2 = doc . add_paragraph ( "这是第二段测试文本,用于添加富文本批注。" ) # 创建空内容批注(后续手动添加富文本) rich_comment = doc . add_comment (    runs = para2 . runs ,    text = "" ,   # 初始为空,后续添加富文本    author = "李四" ,    initials = "LS" ) # 向批注中添加富文本内容 cmt_para = rich_comment . paragraphs [ 0 ]   # 获取批注的默认段落 cmt_para . add_run ( "富文本批注:" )   # 普通文本 cmt_para . add_run ( "重点建议:" ) . bold = True   # 粗体文本 cmt_para . add_run ( "这段文本需要补充案例," ) . italic = True   # 斜体文本 cmt_para . add_run ( "建议参考行业标准。" )   # 普通文本 # 4. 示例3:修改批注元数据(作者、缩写) basic_comment . author = "张小三"   # 修改作者名 basic_comment . initials = "ZXS"   # 修改作者缩写 print ( f"✅ 基础批注元数据已更新:作者= {basic_comment .author } ,缩写= {basic_comment .initials } " ) # 5. 示例4:访问和遍历所有批注 print ( "\n📋 文档中所有批注信息:" ) for idx , comment in enumerate ( doc . comments ) :     print ( f"\n批注 {idx + 1 } :" )     print ( f"  作者: {comment .author } " )     print ( f"  缩写: {comment .initials } " )     # 提取批注的完整文本(兼容富文本)    comment_text = "\n  " . join ( [ para . text for para in comment . paragraphs ] )     print ( f"  内容: {comment_text } " )


from docx . enum . text import WD_ALIGN_PARAGRAPH # 1. 创建一个新的Word文档 doc = Document ( ) # 2. 基础用法:在文档末尾插入图片(单独占一个段落) doc . add_heading ( '1. 基础插入图片' , level = 2 ) # 添加图片,设置宽度为2英寸(高度会按比例自动调整) doc . add_picture ( 'test.png' , width = Inches ( 2 ) ) # 3. 进阶用法:图文混排(图片和文本在同一个段落) doc . add_heading ( '2. 图文混排示例' , level = 2 ) # 创建一个新段落 mixed_paragraph = doc . add_paragraph ( ) # 添加文本到段落开头 mixed_paragraph . add_run ( '这是图片左侧的文本,' ) # 在段落中插入图片(非单独段落) img_run = mixed_paragraph . add_run ( ) img_run . add_picture ( 'test.png' , width = Inches ( 1 ) ) # 在图片右侧添加文本 mixed_paragraph . add_run ( '这是图片右侧的文本。' ) # 4. 自定义图片尺寸+段落对齐 doc . add_heading ( '3. 自定义尺寸与对齐' , level = 2 ) align_para = doc . add_paragraph ( ) align_para . alignment = WD_ALIGN_PARAGRAPH . CENTER   # 段落居中对齐 align_run = align_para . add_run ( ) # 同时设置宽度和高度(注意:可能导致图片变形) align_run . add_picture ( 'test.png' , width = Inches ( 1.5 ) , height = Inches ( 1 ) ) align_para . add_run ( '\n这是居中显示的图片' )


from docx . enum . text import WD_ALIGN_PARAGRAPH from docx . enum . table import WD_TABLE_ALIGNMENT from docx . oxml . ns import qn import os def ensure_test_images ( ) :     # 简单生成测试图片(如果没有的话)     try :         from PIL import Image         for i in range ( 1 , 3 ) :            img_path = f"image {i } .jpg"             if not os . path . exists ( img_path ) :                img = Image . new ( 'RGB' , ( 800 , 600 ) , color = 'lightblue' )                img . save ( img_path )         print ( "测试图片已准备完成" )     except ImportError :         print ( "请安装pillow库:pip install pillow" )        exit ( 1 ) # 生成Word文档 def generate_complex_doc ( ) :     # 创建新文档    doc = Document ( )         # 设置文档默认字体(解决中文显示问题)    doc . styles [ 'Normal' ] . font . name = '微软雅黑'    doc . styles [ 'Normal' ] . _element . rPr . rFonts . set ( qn ( 'w:eastAsia' ) , '微软雅黑' )         # ========== 第一页内容 ==========     # 标题    title = doc . add_heading ( 'XX项目调研报告' , 0 )    title . alignment 51xinku.com = WD_ALIGN_PARAGRAPH . CENTER   # 居中对齐    title_run = title . runs [ 0 ] szfort.com.cn    title_run . font . size = Pt ( 24 )    title_run . font . bold = True         # 副标题/说明    sub_para = doc . add_paragraph ( '报告生成时间:2026年1月  报告编制人:测试用户' )    sub_para . alignment = WD_ALIGN_PARAGRAPH . RIGHT    sub_para_run = sub_para . runs [ 0 ]    sub_para_run . font . size = Pt ( 10 )         # 正文段落  529529.cn    doc . add_heading ( 'qzsxy.com一、项目概述' , level = 1 )    overview_para = doc . add_paragraph ( )    overview_para . add_run ( '本项目针对企业数字化转型需求,重点解决现有业务系统数据孤岛、流程不规范、决策效率低等核心问题。' ) . font . size = Pt ( 12 )psnsd.com    overview_para . add_run ( '\n项目覆盖范围包括:' ) . font . bold = True    overview_para . add_run ( '\n1) 生产管理模块:实现生产流程自动化监控' )    overview_para . add_run ( '\n2) 销售管理模块:打通客户数据与订单系统' )    overview_para . add_run ( '\n3) 财务管理模块:实现财务数据实时汇总分析' )         # 第一个表格:项目基础信息表(2行4列)    doc . add_heading ( '1.1 项目基础信息' , level = 2 )    table1 = doc . add_table ( rows = 2 , cols = 4 )    table1 . alignment = WD_TABLE_ALIGNMENT . CENTER   # 表格居中    table1 . style = 'Table Grid'   # 带边框样式         # 填充表格1表头    hdr_cells = table1 . rows [ 0 ] . cells    hdr_cells [ 0 ] . text = '项目编号'    hdr_cells [ 1 ] . text = '项目名称'    hdr_cells [ 2 ] . text = '启动时间'    hdr_cells [ 3 ] . text = '预计工期'         # 填充表格1内容    row_cells = table1 . rows [ 1 ] . cells    row_cells [ 0 ] . text = 'XM2026001'    row_cells [ 1 ] . text = '企业数字化转型项目'    row_cells [ 2 ] . text = '2026-01-01'    row_cells [ 3 ] . text = '12个月'         # 调整表格1单元格字体     for row in table1 . rows :         for cell in row . cells :             for paragraph in cell . paragraphs :                 for run in paragraph . runs :                    run . font . size = Pt ( 10 )                    run . font . name =qieroom.com '微软雅黑'                    run . _element . rPr . rFonts . set ( qn ( 'w:eastAsia' ) , '微软雅黑' )         # 插入第一张图片 7632.com.cn    doc . add_heading ( '1.2 项目架构图' , level = 2 )    img1_para = doc . add_paragraph ( )    img1_para . alignment = WD_ALIGN_PARAGRAPH . CENTER    img1_run = img1_para . add_run ( )    img1_run . add_picture ( 'image1.jpg' , width = Inches ( 5 ) )   # 图片宽度5英寸    img1_para . add_run ( '\n图1:项目整体架构图' ) . font . size = Pt ( 10 )         # 插入分页符(第一页结束)    doc . add_page_break ( )         # ========== 第二页内容 ==========    doc . add_heading ( '二、项目资源配置' , level = 1 )    resource_para = doc . add_paragraph ( '本项目所需资源包括人力资源、硬件资源、软件资源三大类,具体配置如下表所示:' )         # 第二个表格:资源配置表(5行3列)    table2 = doc . add_table ( rows = 5 , cols = 3 )    table2 . style = 'Table Grid'    table2 . alignment = WD_TABLE_ALIGNMENT . CENTER         # 填充表格2    table2_data = [ 88363.cn         [ '资源类型' , '规格/数量' , '备注' ] ,         [ '人力资源' , '项目经理1人,开发工程师5人,测试工程师2人' , '均为全职' ] ,         [ '服务器' , '8核16G云服务器3台' , '阿里云ECS' ] ,         [ '数据库' , 'MySQL 8.0 主从架构' , '数据实时备份' ] ,         [ '软件授权' , 'Python、Office、开发工具' , '企业版授权' ]     ]         for i , row_data in enumerate ( table2_data ) :        row_cells = table2 . rows [ i ] . cells         for j , cell_text in enumerate ( row_data ) :            row_cells [ j ] . text = cell_text             # 设置单元格字体             for paragraph in row_cells [ j ] . paragraphs :                 for run in paragraph . runs :                    run . font . size = Pt ( 10 )                    run . font . name = '微软雅黑'                    run . _element . rPr . rFonts . set ( qn ( 'w:eastAsia' ) , '微软雅黑' )         # 表头加粗         if i == 0 :LeLeypay.cn             for cell in table2 . rows [ i ] . cells :                 for paragraph in cell . paragraphs :                     for run in paragraph . runs :psnsd.com                        run . font . bold = True         # 插入第二张图片    doc . add_heading ( '2.1 资源部署示意图' , level = 2 )    img2_para = doc . add_paragraph ( )    img2_para . alignment = WD_ALIGN_PARAGRAPH . CENTER    img2_run = img2_para . add_run ( )    img2_run . add_picture ( 'image2.jpg' , width = Inches ( 5 ) )    img2_para . add_run ( '\n图2:资源部署拓扑图' ) . font . size = Pt ( 10 )         # 补充段落内容    doc . add_paragraph ( '资源配置遵循「够用且预留扩展」原则,硬件资源可根据项目进度弹性扩容,人力资源采用「核心+外协」模式保障交付进度。' )         # 插入分页符(第二页结束)    doc . add_page_break ( )         # ========== 第三页内容 ==========    doc . add_heading ( '三、项目风险评估' , level = 1 )    risk_para = doc . add_paragraph ( '通过对技术、人员、成本、进度四个维度的评估,识别出以下核心风险点及应对措施:' )         # 第三个表格:风险评估表(4行4列)    table3 = doc . add_table ( rows = 4 , cols = 4 )    table3 . style = 'Table Grid'    table3 . alignment = WD_TABLE_ALIGNMENT . CENTER        table3_data = [         [ '风险类型' , '风险等级' , '影响范围' , '应对措施' ] ,         [ '技术风险' , '中' , '核心功能模块' , '提前进行技术预研,制定备选方案' ] ,         [ '进度风险' , '低' , '整体交付' , '设置里程碑节点,每周进度复盘' ] ,         [ '成本风险' , '低' , '预算管控' , '建立成本台账,超支提前预警' ]     ]         for i , row_data in enumerate ( table3_data ) :        row_cells = table3 . rows [ i ] . cells         for j , cell_text in enumerate ( row_data ) :            row_cells [ j ] . text = cell_text    qieroom.com         for paragraph in row_cells [ j ] . paragraphs :                 for run in paragraph . runs :                    run . font . size = Pt ( 10 )                    run . font . name = '微软雅黑'                    run . _element . rPr . rFonts . set ( qn ( 'w:eastAsia' ) , '微软雅黑' )         if i == 0 :             for cell in table3 . rows [ i ] . cells :                 for paragraph in cell . paragraphs :                     for run in paragraph . runs :jtwL.com.cn                        run . font . bold = True         # 结尾段落    doc . add_paragraph ( '\n综上所述,本项目整体可控,通过科学的资源配置和风险管控措施,能够保障项目按计划交付并达到预期目标。' )         # 保存文档    doc . save ( '项目报告.docx' )     print ( "项目报告文档已生成" ) if __name__ == '__main__' :


from docx . enum . text import WD_ALIGN_PARAGRAPH from docx . oxml . ns import qn import os # 修改已生成的Word文档 def modify_complex_doc ( file_path ) :sdcnjx.cn     # 打开已生成的文档    doc = Document ( file_path )     print ( "已打开待修改的文档" )         # ========== 1. 修改标题 ==========     # 第一个标题是级别0的标题(主标题)    main_title = doc . paragraphs [ 0 ]    main_title . runs [ 0 ] . text = 'XX企业数字化转型项目终版报告'   # 修改主标题文本    main_title . runs [ 0 ] . font . color . rgb = RGBColor ( 0 , 0 , 255 )   # 标题改为蓝色    main_title . runs [ 0 ] . font . size = Pt ( 20 )    main_title . runs [ 0 ] . font . name = '微软雅黑'    main_title . runs [ 0 ] . _element . rPr . rFonts . set ( qn ( 'w:eastAsia' ) , '微软雅黑' )    main_title . alignment = WD_ALIGN_PARAGRAPH . CENTER         # ========== 2. 修改段落文本 ==========     # 找到"项目概述"下的段落(可通过文本特征定位)     for para in doc . paragraphs :         if '本项目针对企业数字化转型需求' in para . text :             # 清空原有内容,重新写入            para . clear ( )            new_run = para . add_run ( '本项目针对大型制造企业数字化转型需求,重点解决现有业务系统数据孤岛、流程不规范、决策效率低等核心问题。' )            new_run . font . size = Pt ( 12 )            new_run . font . name = '微软雅黑'            new_run . _element . rPr . rFonts . set ( qn ( 'w:eastAsia' ) , '微软雅黑' )             # 追加内容            para . add_run ( '\n项目升级后新增:' )            para . add_run ( '\n4) 供应链管理模块:打通上下游供应商数据' )             break         # ========== 3. 修改表格内容并添加表格名 ==========     # 表格1:项目基础信息表(第一个表格)    table1 = doc . tables [ 0 ]     # 修改项目编号  qzsxy.com    table1 . rows [ 1 ] . cells [ 0 ] . text = 'XM2026001-FINAL'     # 修改预计工期  7632.com.cn    table1 . rows [ 1 ] . cells [ 3 ] . text = '10个月'     # 给修改后的单元格文字标红     for cell in [ table1 . rows [ 1 ] . cells [ 0 ] , table1 . rows [ 1 ] . cells [ 3 ] ] :         for para in cell . paragraphs :             for run in para . runs :winered.cn                run . font . color . rgb = RGBColor ( 255 , 0 , 0 )                run . font . size = Pt ( 11 )                run . font . name = '微软雅黑'                run . _element . rPr . rFonts . set ( qn ( 'w:eastAsia' ) , '微软雅黑' )         # 为表格1添加表格名(表题)     # 找到表格1的位置,在表格上方插入表名    table1_elem = table1 . _element    table_caption1 = doc . add_paragraph ( '表1 项目基础信息表' )    table_caption1 . alignment = WD_ALIGN_PARAGRAPH . CENTER   # 居中对齐    table_caption1 . paragraph_format . space_before = Pt ( 0 )    table_caption1 . paragraph_format . space_after = Pt ( 0 )     # 设置表名字体格式     for run in table_caption1 . runs : wsbbt.com        run . font . size = Pt ( 11 )        run . font . name = '微软雅黑'        run . _element . rPr . rFonts . set ( qn ( 'w:eastAsia' ) , '微软雅黑' )        run . font . bold = True   # 加粗     # 将表名插入到表格上方    table1_elem . addprevious ( table_caption1 . _element )         # 表格2:资源配置表(第二个表格)    table2 = doc . tables [ 1 ]     # 修改服务器配置    table2 . rows [ 3 ] . cells [ 1 ] . text = '16核32G云服务器3台(升级配置)'     # 设置表格2单元格字体     for row in table2 . rows :         for cell in row . cells :             for para in cell . paragraphs :                 for run in para . runs :xyzdh.cn                    run . font . size = Pt ( 11 )                    run . font . name = '微软雅黑'                    run . _element . rPr . rFonts . set ( qn ( 'w:eastAsia' ) , '微软雅黑' )         # 为表格2添加表格名(表题)    table2_elem = table2 . _element    table_caption2 = doc . add_paragraph ( '表2 项目资源配置表' )    table_caption2 . alignment = WD_ALIGN_PARAGRAPH . CENTER     for run in table_caption2 . runs :        run . font . size = Pt ( 11 )        run . font . name = '微软雅黑'        run . _element . rPr . rFonts . set ( qn ( 'w:eastAsia' ) , '微软雅黑' )        run . font . bold = True    table2_elem . addprevious ( table_caption2 . _element )         # ========== 4. 替换图片并规范图名 ==========     # 先准备一张新的测试图片 psnsd.com     from PIL import Image    new_img_path = 'test.png'     if not os . path . exists ( new_img_path ) :        img = Image . new ( 'RGB' , ( 800 , 600 ) , color = 'lightgreen' )        img . save ( new_img_path )         # 找到第一张图片所在的段落(通过图片相关特征)    img_para_index = - 1     for i , para in enumerate ( doc . paragraphs ) :         if '图1:项目整体架构图' in para . text or 'graphicData' in str ( para . _element . xml ) :            img_para_index = i             break         if img_para_index != - 1 :         # 清空段落中的图片和文字,重新添加        img_para = doc . paragraphs [ img_para_index ]        img_para . clear ( )zjtgjx.net                 # 重新添加新图片        img_run = img_para . add_run ( )        img_run . add_picture ( new_img_path , width = Inches ( 5 ) )                 # 添加规范的图名(图题)        img_caption = img_para . add_run ( '\n图1 项目整体架构图(终版)' )        img_caption . font . size = Pt ( 10 )szfort.com.cn        img_caption . font . name = '微软雅黑'        img_caption . _element . rPr . rFonts . set ( qn ( 'w:eastAsia' ) , '微软雅黑' )        img_para . alignment = WD_ALIGN_PARAGRAPH . CENTER         # ========== 5. 添加新内容并为新表格加表名 ==========     # 在第三页风险评估后添加新的段落和表格     # 先找到风险评估标题的位置,在其后添加内容     for i , para in enumerate ( doc . paragraphs ) :         if '三、项目风险评估' in para . text :             # 添加新的子标题            new_sub_heading = doc . add_heading ( '3.1 风险应对预案' , level = 2 )             # 插入到指定位置            doc . paragraphs . insert ( i + 1 , new_sub_heading )                         # 添加新段落            new_para = doc . add_paragraph ( '针对高优先级风险,制定专项应对预案,包括应急响应流程、备用资源调案等,确保风险发生时可快速处置。' )accfj.cn            new_para . alignment = WD_ALIGN_PARAGRAPH . JUSTIFY             for run in new_para . runs :                run . font . size = Pt ( 12 )                run . font . name = '微软雅黑'                run . _element . rPr . rFonts . set ( qn ( 'w:eastAsia' ) , '微软雅黑' )            doc . paragraphs . insert ( i + 2 , new_para )                         # 添加新表格(应急预案表)qyae.cn            new_table = doc . add_table ( rows = 2 , cols = 2 )            new_table . style = 'Table Grid'            new_table . alignment = WD_TABLE_ALIGNMENT . CENTER             # 填充新表格            new_table . rows [ 0 ] . cells [ 0 ] . text = '风险类型'            new_table . rows [ 0 ] . cells [ 1 ] . text = '应急联系人'            new_table . rows [ 1 ] . cells [ 0 ] . text = '技术风险'            new_table . rows [ 1 ] . cells [ 1 ] . text = '张工 13800138000'             # 设置新表格字体             for row in new_table . rows :murnis.cn                 for cell in row . cells :                     for para_cell in cell . paragraphs :                         for run in para_cell . runs :                            run . font . size = Pt ( 10 )


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70048956/viewspace-3109302/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2026-01-28

  • 博文量
    15
  • 访问量
    13027