怎么删除标题下面指定内容的表和段落还有图片信息

python 怎么删除标题下面指定内容的表和段落还有图片信息

@Tiaohh

请您提供更多信息,例如您使用的具体代码或您希望删除的内容的具体示例。

删除标题下面内容??????

@Tiaohh 您可以这样使用代码

doc = aw.Document("input.docx")

title_text = "受试者分布"
title_para = None

for paragraph in doc.get_child_nodes(aw.NodeType.PARAGRAPH, True):
    if title_text in paragraph.get_text() and "_Toc" not in paragraph.get_text():
        title_para = paragraph
        break

next_node = title_para.next_sibling
while next_node is not None:
    if next_node.node_type == aw.NodeType.TABLE:
        next_node.remove()
    elif next_node.node_type == aw.NodeType.PARAGRAPH:
        paragraph_text = next_node.get_text()
        # 检查段落是否包含要删除的特定内容。
        if "受试者" in paragraph_text:
            next_node.remove()
        if "受试者一般信息分组描述" in paragraph_text:
            break  # 如果到达下一个标题节点,则停止。
    elif next_node.node_type == aw.NodeType.SHAPE:
        if next_node.shape_type == aw.ShapeType.IMAGE:
            next_node.remove()
    else:
        break  # 如果我们到达一个不需要删除的节点,则停止。
    next_node = title_para.next_sibling

doc.save("output.docx")

或者你也可以简化程序,删除所有内容,直到找不到新标题或其他内容。

我需要检测到 下一个标题停止删除

还有一个问题 我的json文件是这样的

{
    "1标题页": {
        "PRT": [],
        "sap": [],
        "content": ""
    },
    "临床试验报告签字页": {
        "PRT": [],
        "sap": [],
        "content": ""
    },
    "2试验报告摘要": {
        "PRT": [],
        "sap": [],
        "content": ""
    },
    "3目录": {
        "PRT": [],
        "sap": [],
        "content": ""
    },
    "表目录": {
        "PRT": [],
        "sap": [],
        "content": ""
    },
    "图目录": {
        "PRT": [],
        "sap": [],
        "content": ""
    },
    "4缩略语和术语": {
        "PRT": [],
        "sap": [],
        "content": ""
    },
    "5伦理学": {
        "PRT": [],
        "sap": [],
        "content": ""
    },
    "5.1伦理委员会": {
        "PRT": [],
        "sap": [],
        "content": ""
    },
    "5.2研究的管理规范和伦理学准则": {
        "PRT": [],
        "sap": [],
        "content": ""
    },
    "5.3受试者知情和同意": {
        "PRT": [],
        "sap": [],
        "content": ""
    },
    "6研究者和试验管理结构": {
        "PRT": [],
        "sap": [],
        "content": ""
    },
    "7前言": {
        "PRT": [],
        "sap": [],
        "content": ""
    },
    "7.1研究背景": {
        "useProtocolContentDirectly": true,
        "PRT": [
            "2.1研究背景"
        ],
        "sap": [],
        "content": "北京泰德制药有限公司正在开发 TDI01,一种新的研究性选择性 Rho 关联卷曲螺旋蛋白激酶 2(ROCK2)抑制剂,适用于治疗特发性肺纤维化(IPF)。IPF 是一种病因不明,慢性进行性纤维化性间质性肺炎,病变局限在肺脏,好发于中老年男性人群,主要表现为进行性加重的呼吸困难,伴限制性通气功能障碍和气体交换障碍,导致低氧血症、甚至呼吸衰竭,预后差,其肺组织学和胸部高分辨率 CT(HRCT)表现为普通型间质性肺炎(UIP)[1,2,3]。IPF 的自然病程通常为肺功能隐匿性下降,平均在初始诊断后大约 4 年内进展为呼吸衰竭甚至死亡[4]。据报道,IPF 患者的中位生存期为 2-5 年[4,5]。\nIPF 的治疗一般需联合使用以下措施:支持治疗、使用特定药物、考虑参与临床试验、肺移植。目前可用来治疗 IPF 的药物都不能治愈该病。但有两种抗纤维化药物可延缓疾病进展、降低急性加重频率,即尼达尼布和吡非尼酮[5,6,7]。尼达尼布是针对多种酪氨酸激酶的一种受体阻滞剂,其阻断这些酪氨酸激酶介导的促纤维形成生长因子(如 PDGF、VEGF、FGF)的作用,似乎可延缓 IPF 疾病进展。吡非尼酮是一种新的具有广谱抗纤维化作用的吡啶酮类化合物,具有抗纤维化和抗炎作用,主要用于轻、中度特发性纤维化。这些药物仅能部分延缓肺功能恶化速度,不能明显改善 IPF 患者预后。另外,这些药物都有相对明显的副作用。IPF 治疗的医疗需求尚未得到满⾜,更有效、副作用更低的药物的有待开发[8]。"
    },
    "7.2ROCK2抑制剂": {
        "PRT": [],
        "sap": [],
        "content": ""
    },
    "7.3药物背景": {
        "PRT": [],
        "sap": [],
        "content": ""
    },
    "8试验目的和终点": {
        "PRT": [],
        "sap": [],
        "content": ""
    },
    "8.1试验目的": {
        "useProtocolContentDirectly": true,
        "PRT": [
            "3.1主要目的",
            "3.2次要目的",
            "3.3探索性目的"
        ],
        "sap": [],
        "content": "评价不同剂量的 TDI01 混悬液与安慰剂组相比在治疗 IPF 患者中的临床疗效。评价不同剂量的 TDI01 混悬液治疗 IPF 患者的安全性和耐受性。\n评价 TDI01 在 IPF 患者中的群体药代动力学特征。\n探索影响 TDI01 混悬液治疗 IPF 患者疗效的生物标志物。探索 TDI01 混悬液治疗 IPF 患者的影像学改善。"
    },
    "8.2试验终点": {
        "useProtocolContentDirectly": true,
        "PRT": [
            "5.1主要终点与主要估计目标主要疗效终点",
            "5.2次要疗效终点",
            "5.3探索性终点",
            "5.4群体药代动力学终点"
        ],
        "sap": [],
        "content": "24 周时,用力肺活量(FVC)(ml)较基线的平均变化。主要估计目标\n本研究主要估计目标的 5 个属性如下:\n1.目标人群:特发性肺纤维化(IPF)患者。\n2.治疗:试验组(TDI01 400mg 组、TDI01 200mg 组 );对照组(安慰剂组)。\n3.目标变量:第 24 周时,FVC(ml)较基线的平均变化。\n4.伴发事件及处理策略:\n处理策略\r伴发事件\n处理策略\n伴发事件\n<table cellspacing=\"0\" cellpadding=\"0\" style=\"margin-left:57.7pt; border:0.75pt solid #000000; border-collapse:collapse\"><tr style=\"height:73.3pt\"><td style=\"width:165.25pt; border-right-style:solid; border-right-width:0.75pt; border-bottom-style:solid; border-bottom-width:0.75pt; vertical-align:top\"><p style=\"margin-top:0pt; margin-bottom:0pt; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体\">&#xa0;</span></p><p style=\"margin-top:2.55pt; margin-bottom:0pt; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体\">&#xa0;</span></p><p style=\"margin-top:0pt; margin-left:5.35pt; margin-bottom:0pt; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:-0.05pt\">终止研究治疗 </span><span style=\"font-family:'Times New Roman'; letter-spacing:1.15pt\">- </span><span style=\"font-family:宋体; letter-spacing:2.5pt\">因</span><span style=\"font-family:'Times New Roman'\">AE-</span><span style=\"font-family:'Times New Roman'; letter-spacing:-0.25pt\">IPF</span></p></td><td style=\"width:203.9pt; border-left-style:solid; border-left-width:0.75pt; border-bottom-style:solid; border-bottom-width:0.75pt; vertical-align:top\"><p style=\"margin-top:5.4pt; margin-left:5.25pt; margin-bottom:0pt; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:0.25pt\">采用假想策略,因</span><span style=\"font-family:'Times New Roman'\">AE-IPF</span><span style=\"font-family:'Times New Roman'; letter-spacing:-0.65pt\"> </span><span style=\"font-family:宋体; letter-spacing:-0.1pt\">提前终止研究</span></p><p style=\"margin:0pt 9.5pt 0pt 5.25pt; line-height:24.5pt; widows:0; orphans:0\"><span style=\"font-family:宋体; font-size:10.5pt; letter-spacing:-0.1pt\">治疗后的数据将不再使用,仅使用伴发事件发生前的数据进行推论</span></p></td></tr><tr style=\"height:97.85pt\"><td style=\"width:165.25pt; border-top-style:solid; border-top-width:0.75pt; border-right-style:solid; border-right-width:0.75pt; border-bottom-style:solid; border-bottom-width:0.75pt; vertical-align:top\"><p style=\"margin-top:0pt; margin-bottom:0pt; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体\">&#xa0;</span></p><p style=\"margin-top:2.55pt; margin-bottom:0pt; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体\">&#xa0;</span></p><p style=\"margin:0pt 13.3pt 0pt 5.35pt; line-height:179%; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:-0.1pt\">终止研究治疗 </span><span style=\"font-family:'Times New Roman'; letter-spacing:1pt\">- </span><span style=\"font-family:宋体; letter-spacing:-0.95pt\">因除 </span><span style=\"font-family:'Times New Roman'\">AE-IPF</span><span style=\"font-family:'Times New Roman'; letter-spacing:-0.25pt\"> </span><span style=\"font-family:宋体\">以</span><span style=\"font-family:宋体; letter-spacing:-0.1pt\">外的其他原因</span></p></td><td style=\"width:203.9pt; border-top-style:solid; border-top-width:0.75pt; border-left-style:solid; border-left-width:0.75pt; border-bottom-style:solid; border-bottom-width:0.75pt; vertical-align:top\"><p style=\"margin:5.5pt 9.5pt 0pt 5.25pt; line-height:179%; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:0.3pt\">采用假想策略,因</span><span style=\"font-family:'Times New Roman'\">AE-IPF </span><span style=\"font-family:宋体\">以外的其他原</span><span style=\"font-family:宋体; letter-spacing:-0.1pt\">因提前终止研究治疗后的数据将不再使 用,仅使用伴发事件发生前的数据进行推</span></p><p style=\"margin-top:0pt; margin-left:5.25pt; margin-bottom:0pt; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:-0.5pt\">论</span></p></td></tr><tr style=\"height:73.3pt\"><td style=\"width:165.25pt; border-top-style:solid; border-top-width:0.75pt; border-right-style:solid; border-right-width:0.75pt; border-bottom-style:solid; border-bottom-width:0.75pt; vertical-align:top\"><p style=\"margin-top:3.95pt; margin-bottom:0pt; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体\">&#xa0;</span></p><p style=\"margin:0.05pt 13.3pt 0pt 5.35pt; line-height:185%; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:-0.15pt\">暂停治疗 </span><span style=\"font-family:'Times New Roman'; letter-spacing:1pt\">- </span><span style=\"font-family:宋体; letter-spacing:-0.95pt\">治疗 </span><span style=\"font-family:'Times New Roman'\">24</span><span style=\"font-family:'Times New Roman'; letter-spacing:-0.35pt\"> </span><span style=\"font-family:宋体; letter-spacing:-0.7pt\">周时因 </span><span style=\"font-family:'Times New Roman'\">AE- </span><span style=\"font-family:'Times New Roman'; letter-spacing:-0.2pt\">IPF</span></p></td><td style=\"width:203.9pt; border-top-style:solid; border-top-width:0.75pt; border-left-style:solid; border-left-width:0.75pt; border-bottom-style:solid; border-bottom-width:0.75pt; vertical-align:top\"><p style=\"margin:5.35pt 8.4pt 0pt 5.25pt; line-height:179%; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:-0.25pt\">采用假想策略,因治疗 </span><span style=\"font-family:'Times New Roman'\">24</span><span style=\"font-family:'Times New Roman'; letter-spacing:-0.7pt\"> </span><span style=\"font-family:宋体; letter-spacing:1.25pt\">周时</span><span style=\"font-family:'Times New Roman'\">AE-IPF</span><span style=\"font-family:'Times New Roman'; letter-spacing:-0.65pt\"> </span><span style=\"font-family:宋体\">暂</span><span style=\"font-family:宋体; letter-spacing:-0.15pt\">停研究治疗后的数据将不再使用,仅使用</span></p><p style=\"margin-top:0pt; margin-left:5.25pt; margin-bottom:0pt; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:-0.15pt\">伴发事件发生前的数据进行推论</span></p></td></tr><tr style=\"height:97.75pt\"><td style=\"width:165.25pt; border-top-style:solid; border-top-width:0.75pt; border-right-style:solid; border-right-width:0.75pt; border-bottom-style:solid; border-bottom-width:0.75pt; vertical-align:top\"><p style=\"margin:5.35pt 5.05pt 0pt 5.35pt; text-align:justify; line-height:179%; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:-0.7pt\">除 </span><span style=\"font-family:'Times New Roman'\">AE-IPF</span><span style=\"font-family:'Times New Roman'; letter-spacing:-0.65pt\"> </span><span style=\"font-family:宋体; letter-spacing:-0.2pt\">外,用于治疗 </span><span style=\"font-family:'Times New Roman'\">IPF</span><span style=\"font-family:'Times New Roman'; letter-spacing:-0.65pt\"> </span><span style=\"font-family:宋体\">的糖皮质激素</span><span style=\"font-family:'Times New Roman'\">&gt; 15 mg/d </span><span style=\"font-family:宋体\">泼尼松或同等剂量的其他糖皮质激素治疗</span><span style=\"font-family:'Times New Roman'; letter-spacing:0.85pt\">, </span><span style=\"font-family:宋体; letter-spacing:-0.25pt\">使用</span></p><p style=\"margin-top:0.05pt; margin-left:5.35pt; margin-bottom:0pt; text-align:justify; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:-0.6pt\">时间大于 </span><span style=\"font-family:'Times New Roman'; letter-spacing:-0.1pt\">1</span><span style=\"font-family:'Times New Roman'\"> </span><span style=\"font-family:宋体; letter-spacing:-0.45pt\">周,且判定为 </span><span style=\"font-family:'Times New Roman'; letter-spacing:-0.25pt\">IPD</span></p></td><td style=\"width:203.9pt; border-top-style:solid; border-top-width:0.75pt; border-left-style:solid; border-left-width:0.75pt; border-bottom-style:solid; border-bottom-width:0.75pt; vertical-align:top\"><p style=\"margin:5.35pt 9.5pt 0pt 5.25pt; text-align:justify; line-height:179%; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:-0.1pt\">采用假想策略,伴发事件发生后的数据将不再使用,仅使用伴发事件发生前的数据</span><span style=\"font-family:宋体; letter-spacing:-0.2pt\">进行推论</span></p></td></tr><tr style=\"height:73.4pt\"><td style=\"width:165.25pt; border-top-style:solid; border-top-width:0.75pt; border-right-style:solid; border-right-width:0.75pt; border-bottom-style:solid; border-bottom-width:0.75pt; vertical-align:top\"><p style=\"margin:13.5pt 15.1pt 0pt 5.35pt; text-align:justify; line-height:120%; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:0.6pt\">开始使用</span><span style=\"font-family:'Times New Roman'\">AF</span><span style=\"font-family:'Times New Roman'; letter-spacing:-0.7pt\"> </span><span style=\"font-family:宋体\">治疗(尼拉尼布、</span><span style=\"font-family:宋体; letter-spacing:0.3pt\">吡非尼酮以及其他</span><span style=\"font-family:'Times New Roman'\">ROCK2</span><span style=\"font-family:'Times New Roman'; letter-spacing:-0.7pt\"> </span><span style=\"font-family:宋体\">抑制剂)</span><span style=\"font-family:'Times New Roman'\">,</span><span style=\"font-family:宋体; letter-spacing:-0.4pt\">且判定为 </span><span style=\"font-family:'Times New Roman'\">IPD</span></p></td><td style=\"width:203.9pt; border-top-style:solid; border-top-width:0.75pt; border-left-style:solid; border-left-width:0.75pt; border-bottom-style:solid; border-bottom-width:0.75pt; vertical-align:top\"><p style=\"margin:5.35pt 9.5pt 0pt 5.25pt; line-height:179%; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:0.2pt\">采用假想策略,开始使用</span><span style=\"font-family:'Times New Roman'\">AF </span><span style=\"font-family:宋体\">治疗后的数</span><span style=\"font-family:宋体; letter-spacing:-0.15pt\">据将不再使用,仅使用伴发事件发生前的</span></p><p style=\"margin-top:0.1pt; margin-left:5.25pt; margin-bottom:0pt; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:-0.2pt\">数据进行推论</span></p></td></tr><tr style=\"height:73.3pt\"><td style=\"width:165.25pt; border-top-style:solid; border-top-width:0.75pt; border-right-style:solid; border-right-width:0.75pt; border-bottom-style:solid; border-bottom-width:0.75pt; vertical-align:top\"><p style=\"margin-top:7.9pt; margin-bottom:0pt; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体\">&#xa0;</span></p><p style=\"margin:0pt 15.1pt 0pt 5.35pt; line-height:120%; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:0.6pt\">停止使用</span><span style=\"font-family:'Times New Roman'\">AF</span><span style=\"font-family:'Times New Roman'; letter-spacing:-0.7pt\"> </span><span style=\"font-family:宋体\">治疗(尼拉尼布、</span><span style=\"font-family:宋体; letter-spacing:-0.1pt\">吡非尼酮)</span></p></td><td style=\"width:203.9pt; border-top-style:solid; border-top-width:0.75pt; border-left-style:solid; border-left-width:0.75pt; border-bottom-style:solid; border-bottom-width:0.75pt; vertical-align:top\"><p style=\"margin:5.35pt 9.5pt 0pt 5.25pt; line-height:178%; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:0.2pt\">采用假想策略,停止使用</span><span style=\"font-family:'Times New Roman'\">AF </span><span style=\"font-family:宋体\">治疗后的数</span><span style=\"font-family:宋体; letter-spacing:-0.15pt\">据将不再使用,仅使用伴发事件发生前的</span></p><p style=\"margin-top:0.25pt; margin-left:5.25pt; margin-bottom:0pt; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:-0.2pt\">数据进行推论</span></p></td></tr><tr style=\"height:73.4pt\"><td style=\"width:165.25pt; border-top-style:solid; border-top-width:0.75pt; border-right-style:solid; border-right-width:0.75pt; vertical-align:top\"><p style=\"margin-top:0pt; margin-bottom:0pt; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体\">&#xa0;</span></p><p style=\"margin-top:2.55pt; margin-bottom:0pt; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体\">&#xa0;</span></p><p style=\"margin-top:0pt; margin-left:5.35pt; margin-bottom:0pt; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:-1.35pt\">因 </span><span style=\"font-family:'Times New Roman'\">IPF</span><span style=\"font-family:'Times New Roman'; letter-spacing:-0.3pt\"> </span><span style=\"font-family:宋体; letter-spacing:-0.15pt\">导致死亡</span></p></td><td style=\"width:203.9pt; border-top-style:solid; border-top-width:0.75pt; border-left-style:solid; border-left-width:0.75pt; vertical-align:top\"><p style=\"margin-top:5.35pt; margin-left:5.25pt; margin-bottom:0pt; widows:0; orphans:0; font-size:10.5pt\"><span style=\"font-family:宋体; letter-spacing:-0.4pt\">采用复合策略,因 </span><span style=\"font-family:'Times New Roman'; letter-spacing:-0.1pt\">IPF</span><span style=\"font-family:'Times New Roman'; letter-spacing:0.4pt\"> </span><span style=\"font-family:宋体; letter-spacing:-0.15pt\">导致死亡后的数据</span></p><p style=\"margin:0pt 9.5pt 0pt 5.25pt; line-height:24.5pt; widows:0; orphans:0\"><span style=\"font-family:宋体; font-size:10.5pt; letter-spacing:-0.1pt\">不适用,伴发事件发生后的评估将赋值为</span><span style=\"font-family:宋体; font-size:10.5pt; letter-spacing:-0.15pt\">对照组的数据 </span><span style=\"font-family:'Times New Roman'; font-size:10.5pt\">J2R </span><span style=\"font-family:宋体; font-size:10.5pt\">以表明治疗失败</span></p></td></tr></table>5.群体层面汇总: TDI01 400mg 组对比安慰剂组、TDI01 200mg 组对比安慰剂组在给药第 24 周时 FVC(ml)较基线的平均变化的组间最小二乘均值差异。\n安全性终点:\n不良事件、高分辨率 CT (HRCT)、实验室检查、12 导联心电图、生命体征、体格检查;\n第 12 周时,FVC 占预计值百分比(FVC%)、用力肺活量(FVC)、6MWD、\nSGRQ 评分和 DLco(Hb 校正)较基线的变化;\n第 24 周时, FVC 占预计值百分比(FVC%)、6MWD、SGRQ 评分和 DLco\n(Hb 校正)较基线的变化;\n第 32、40、52 周时,FVC、FVC 占预计值百分比(FVC%)较基线的变化;\n第 24、52 周时,与基线相比,FVC 占预计值百分比(FVC%)绝对值下降\n>10%的受试者比例;\n至首次发生 IPF 急性加重的时间(天);\n第 24、52 周时,IPF 急性加重频率;\n至 IPF 疾病相关死亡或全因死亡时间;\n至疾病进展时间(与基线相比,FVC%的绝对值下降>10%、肺移植或死亡,以先发生者为准);\n24 周、52 周时,IPF 疾病相关死亡率、全因死亡率。\n采集时间点:在 D1、W8、W12、W24 和 W52 给药前 1h 内采血。\n检测方法:化学发光免疫分析法(ECLIA)和酶联免疫吸附法(ELISA)。\n外周血中纤维化标志物 MMP-7、AREG 的变化;\n外周血中炎症标志物 IL-17 的变化(可选);\n外周血中肺损伤标志物 SP-D、CYFRA 21-1、KL-6 的变化;\nHRCT 中的胸部影像学进展,如新发磨玻璃影伴有牵张性细支气管扩张、新发细网格影、粗网格影范围增大或程度加剧、新发蜂窝影或原有蜂窝影加剧等变化。\n对本研究中获得的 TDI01 的血浆浓度数据,以评估 TDI01 在 IPF 患者中的药物暴露情况。将使用非线性混合效应模型(Nonlinear Mixed-effects Modeling Approach)进行群体药代动力学分析,以表征 TDI01 在 IPF 患者中的 PK 特征。并且评估各种内在因素和外在因素对 TDI01 PK 参数的影响。\n如果数据允许,将基于建立的最终模型的 PK 参数估计值,对受试者的个体暴露参数(例如, 总暴露量、峰浓度、谷浓度等)进行估算,用于进一步的量-效分 析,包括暴露-疗效及暴露-安全性的相关性探索分析。这些分析结果将单独报告。临床试验总结报告将对药代动力学参数进行分析。\nPK 参数终点:\nCmin;C4h 。\nPK 采血时间点:\nD1、D7、W3、W8:给药前 1h 内、给药后 4h(给药后采集的所有样品时间窗\n±60min;W3 和 W8 的给药后 4h 采集不作硬性要求);\nW12、W24:给药前 1h 内采集血样进行药代动力学检测。\n计划外:当发生严重不良事件或由于不良事件暂停用药/终止研究治疗/退出研究时,如条件允许,尽快在访视时采集血样。"
    },
    "9试验设计": {
        "PRT": [],
        "sap": [],
        "content": ""
    },

我需要通过代码写到指定的标题下面 \n为什么没有识别出来换行,还有字体也没有设置成功。还有就是遇到每一个\n都需要缩进 # -- coding: utf-8 --

import aspose.words as aw
import json
import re

# 加载Aspose.Words的许可证
lic = aw.License()
lic_path = "../../Aspose.Total.Product.Family.lic"
lic.set_license(lic_path)


def remove_section_numbers(title):
    """移除标题前的章节序号,如 '7.1研究背景' -> '研究背景'"""
    # 匹配开头的数字和点号
    return re.sub(r'^[\d\.]+\s*', '', title)


def contains_html(text):
    """检查文本是否包含HTML标记"""
    return re.search(r'<[^>]+>', text) is not None


def contains_chinese(text):
    """检查文本是否包含中文字符"""
    return bool(re.search(r'[\u4e00-\u9fff]', text))


def fix_run_font(run):
    """根据文本内容设置正确的字体"""
    run_text = run.get_text()
    if contains_chinese(run_text):
        # 包含中文的文本,使用宋体
        run.font.name = "宋体"
    else:
        # 纯英文或数字文本,使用Times New Roman
        run.font.name = "Times New Roman"


def process_tables_in_document(doc, start_node=None):
    """处理文档中的表格,设置五号字体(10.5磅)"""
    # 获取要处理的节点集合
    nodes_to_process = []
    if start_node:
        # 如果指定了起始节点,则从该节点开始收集后续节点
        current = start_node
        while current:
            nodes_to_process.append(current)
            current = current.next_sibling
    else:
        # 否则处理整个文档
        nodes_to_process = doc.get_child_nodes(aw.NodeType.ANY, True)

    # 查找并处理表格
    for node in nodes_to_process:
        if node.node_type == aw.NodeType.TABLE:
            table = node.as_table()

            # 设置表格属性
            table.allow_auto_fit = True

            # 遍历表格中的所有单元格
            for row in table.rows:
                for cell in row.as_row().cells:
                    # 遍历单元格中的所有段落
                    for para in cell.as_cell().paragraphs:
                        # 设置段落格式 - 表格内段落不需要段前段后间距
                        para = para.as_paragraph()
                        para.paragraph_format.space_before = 0
                        para.paragraph_format.space_after = 0
                        # 遍历段落中的所有文本运行
                        for run in para.runs:
                            # 设置字体为五号(10.5磅)
                            run = run.as_run()
                            run.font.size = 10.5
                            # 根据文本内容设置字体
                            fix_run_font(run)


def fix_document_fonts(doc):
    """修复文档中所有文本的字体"""
    for run in doc.get_child_nodes(aw.NodeType.RUN, True):
        run = run.as_run()
        fix_run_font(run)


def fix_paragraph_spacing(doc, start_node=None):
    """修复文档中的段落间距"""
    # 获取要处理的节点集合

    nodes_to_process = []
    if start_node:
        # 如果指定了起始节点,则从该节点开始收集后续节点
        current = start_node
        while current:
            nodes_to_process.append(current)
            current = current.next_sibling
    else:
        # 否则处理整个文档
        nodes_to_process = doc.get_child_nodes(aw.NodeType.ANY, True)

    # 遍历所有段落,确保正确的段落间距
    for node in nodes_to_process:
        if node.node_type == aw.NodeType.PARAGRAPH:
            para = node.as_paragraph()

            # 跳过标题段落和表格内段落
            if para.paragraph_format.outline_level in [0, 1, 2, 3, 4, 5] or para.is_in_cell:
                continue

            # 检查前一个节点是否为表格
            prev_node = para.previous_sibling
            if prev_node and prev_node.node_type == aw.NodeType.TABLE:
                # 表格后的第一个段落
                para.paragraph_format.space_before = 0  # 不需要段前间距

            # 设置段后间距为1.5行
            para.paragraph_format.space_after = 30

            # 设置首行缩进2字符
            para.paragraph_format.character_unit_first_line_indent = 2



def insert_content_from_json(template_path, output_path, json_file_path):
    """从JSON文件读取内容,并插入到文档中对应标题后"""
    # 加载模板文档
    doc = aw.Document(template_path)

    # 加载JSON数据
    with open(json_file_path, 'r', encoding='utf-8') as f:
        content_data = json.load(f)

    # 更新列表标签,确保大纲级别正确
    doc.update_list_labels()

    # 创建DocumentBuilder
    builder = aw.DocumentBuilder(doc)
    builder.font.size = 12  # 小四字号

    processed_headings = set()  # 用于跟踪已处理的标题

    # 遍历文档中的所有节
    for section in doc.sections:
        sect = section.as_section()
        # 遍历节中的所有节点
        for node in sect.body.get_child_nodes(aw.NodeType.ANY, True):
            # 只处理段落节点
            if node.node_type == aw.NodeType.PARAGRAPH:
                para = node.as_paragraph()
                # 检查是否是标题段落(具有大纲级别0-5)
                if para.paragraph_format.outline_level in [0, 1, 2, 3, 4, 5]:
                    full_title = para.get_text().strip()

                    # 获取不带序号的标题,用于匹配
                    title_no_numbers = remove_section_numbers(full_title)

                    # 在JSON中寻找匹配的标题
                    for json_title, data in content_data.items():
                        # 移除JSON标题中的序号进行比较
                        json_title_no_numbers = remove_section_numbers(json_title)

                        # 检查是否匹配且尚未处理
                        if ((json_title_no_numbers == title_no_numbers or
                             json_title == full_title) and
                                full_title not in processed_headings):

                            # 检查内容是否存在且非空
                            if "content" in data and data["content"].strip():
                                try:
                                    content = data["content"]

                                    print(f"正在处理标题: {full_title} -> 匹配JSON标题: {json_title}")

                                    # 检查内容是否包含HTML
                                    has_html = contains_html(content)

                                    # 在段落后插入新段落
                                    new_para = aw.Paragraph(doc)
                                    para.parent_node.insert_after(new_para, para)

                                    # 移动到新段落
                                    builder.move_to(new_para)

                                    # 设置段落格式
                                    builder.paragraph_format.space_after = 30  # 1.5行的段后间距
                                    para.paragraph_format.character_unit_first_line_indent = 2

                                    if has_html:
                                        # 包含HTML标签的内容
                                        print(f"  - 检测到HTML内容,使用HTML插入模式")

                                        # 简化的HTML,不设置字体
                                        css = """
                                        <style>
                                        /* 段落设置 */
                                        p:not(table p), div:not(table div) {
                                            font-size: 12pt !important;
                                            margin-bottom: 30pt !important; /* 1.5行的段后间距 */
                                            text-indent: 2em !important; /* 首行缩进2字符 */
                                        }

                                        /* 表格设置 */
                                        table, td, th, table span, table div, table p {
                                            font-size: 10.5pt !important;
                                            margin: 0 !important; /* 表格内无段前段后间距 */
                                        }

                                        /* 表格无边距 */
                                        table {
                                            margin-top: 0 !important;
                                            margin-bottom: 0 !important;
                                        }
                                        </style>
                                        """

                                        styled_content = css + content

                                        # 插入HTML内容
                                        builder.insert_html(styled_content)

                                        try:
                                            # 处理刚插入内容的表格和段落
                                            process_tables_in_document(doc, new_para)
                                            fix_paragraph_spacing(doc, new_para)
                                        except Exception as e:
                                            print(f"处理表格或段落格式时出错: {str(e)}")
                                    else:
                                        # 纯文本内容
                                        content_parts = content.split('\n')
                                        for i, part in enumerate(content_parts):
                                            if i > 0:  # 不是第一部分则先添加换行
                                                builder.writeln()
                                                # 每段都重新设置格式
                                                builder.paragraph_format.first_line_indent = 21
                                                builder.paragraph_format.space_after = 30  # 1.5行的段后间距
                                                builder.font.size = 12
                                            builder.write(part)

                                    # 标记该标题已处理
                                    processed_headings.add(full_title)
                                    break  # 找到匹配的标题后退出内部循环

                                except Exception as e:
                                    print(f"处理标题 {full_title} 时出错: {str(e)}")
                                    import traceback
                                    traceback.print_exc()
                            else:
                                print(f"标题 {full_title} 匹配的JSON项没有内容或内容为空")

    # 最终修复所有文本的字体
    fix_document_fonts(doc)

    # 保存修改后的文档
    doc.save(output_path)
    print(f"已处理 {len(processed_headings)} 个标题")

@Tiaohh 使用 \n,您可以使用 styled_content = css + "<div style='white-space: pre'>" + content_data + "</div>" 或将所有 \n 改为 <br/>

这个我需要检测到下一个标题 停止删除 下一个标题不能指定

@Tiaohh 要检测下一个标题,您需要找到一些可以帮助您找到它的特定参数。 例如,可以是文档中用于标题的特定样式。

就是标题等级样式。怎么修改呢

还有一个问题超链接样式怎么删除。 就是内容有这种的HYPERLINK
这个是批注的 node.get_child_nodes(aw.NodeType.COMMENT, True).clear()。就是内容有这种的HYPERLINK怎么清楚这个HYPERLINK信息

或者写入的时候怎么写入超链接格式的。 用我给的代码案例

@Tiaohh 您可以创建新样式,也可以通过名称或样式标识符获取样式并将其设置到段落中。

@Tiaohh 如果说到超链接字段,可以通过下面的示例访问该字段格式:

doc = aw.Document()
builder = aw.DocumentBuilder(doc)

builder.write("For more information, please visit the ")

builder.font.color = aspose.pydrawing.Color.blue
builder.font.underline = aw.Underline.SINGLE
builder.insert_hyperlink("Google website", "https://www.google.com", False)
builder.font.clear_formatting()
builder.writeln(".")

doc.save("InsertHyperlink.docx")

doc = aw.Document("InsertHyperlink.docx")

hyperlink = doc.range.fields[0].as_field_hyperlink()
self.assertEqual("https://www.google.com", hyperlink.address)

field_code = hyperlink.start.next_sibling
self.assertEqual('HYPERLINK "https://www.google.com"', field_code.get_text().strip())

field_result = hyperlink.separator.next_sibling.as_run()

self.assertEqual(aspose.pydrawing.Color.blue.to_argb(), field_result.font.color.to_argb())
self.assertEqual(aw.Underline.SINGLE, field_result.font.underline)
self.assertEqual("Google website", field_result.get_text().strip())
def aw_extract_headings_and_contents_table_dict_id(file):
    doc = aw.Document(file)
    current_level = 0
    data = []
    doc.update_list_labels()
    stack = []
    has_outline = False  # 用于标记是否存在大纲级别

    # 首先检查文档是否包含大纲级别的段落
    for s in doc.sections:
        sect = s.as_section()
        for node in sect.body.get_child_nodes(aw.NodeType.ANY, True):
            if node.node_type == aw.NodeType.PARAGRAPH:
                node = node.as_paragraph()
                if node.paragraph_format.outline_level in [0, 1, 2, 3, 4, 5]:
                    has_outline = True
                    break

    # 如果没有大纲级别,则读取整个文档内容
    if not has_outline:
        all_content = ""
        for s in doc.sections:
            sect = s.as_section()
            for node in sect.body.get_child_nodes(aw.NodeType.ANY, True):
                if node.node_type == aw.NodeType.PARAGRAPH:
                    node = node.as_paragraph()
                    node.get_child_nodes(aw.NodeType.COMMENT, True).clear()
                    if node.get_text().strip() and not node.get_ancestor(
                            aw.NodeType.FIELD_START) and not node.get_ancestor(
                        aw.NodeType.TABLE):
                        clean_text = remove_illegal_characters(node.get_text().strip())
                        all_content += clean_text + "\n"

                elif node.node_type == aw.NodeType.TABLE:
                    parent_node = node.as_table()
                    table_content = aw_read_table_id(parent_node)
                    all_content += table_content.replace(' ', '') + "\n"

        return {"document_content": all_content}

    # 如果有大纲级别,使用原来的处理逻辑
    for s in doc.sections:
        sect = s.as_section()
        for node in sect.body.get_child_nodes(aw.NodeType.ANY, True):
            if node.node_type == aw.NodeType.PARAGRAPH:
                node = node.as_paragraph()
                if node.paragraph_format.outline_level in [0, 1, 2, 3, 4, 5]:
                    if node.node_type == aw.NodeType.FIELD_START:
                        continue
                    level = int(node.paragraph_format.outline_level) + 1
                    if level > current_level:
                        stack.append((current_level, data))
                        data = []
                        current_level = level
                    elif level < current_level:
                        while stack and stack[-1][0] >= level:
                            old_level, old_data = stack.pop()
                            data = old_data + data
                            current_level = old_level
                    label = ''
                    if node.list_format.is_list_item:
                        label = node.list_label.label_string
                    node.get_child_nodes(aw.NodeType.COMMENT, True).clear()
                    text_without_comments = node.get_text().strip()
                    data.append({label + text_without_comments: ''})
                else:
                    node.get_child_nodes(aw.NodeType.COMMENT, True).clear()
                    label = ''
                    if node.list_format.is_list_item:
                        label = node.list_label.label_string
                    node.get_child_nodes(aw.NodeType.COMMENT, True).clear()

                    if node.get_text().strip() and not node.get_ancestor(
                            aw.NodeType.TABLE) and not node.get_ancestor(aw.NodeType.FIELD_START) and data:
                        clean_text = remove_illegal_characters(node.get_text().strip())
                        data[-1][list(data[-1].keys())[0]] += label + clean_text.replace('  SEQ 表 \* ARABIC ',
                                                                                         '').replace(
                            'TOC \h \c "表" HYPERLINK \l "_Toc14741"', '').replace(
                            '\u0013 SEQ 图 \\* ARABIC \u00141\u0015 ', '') + "\n"

            if node.node_type == aw.NodeType.TABLE:
                parent_node = node.as_table()
                table_content = aw_read_table_id(parent_node)
                if data:
                    data[-1][list(data[-1].keys())[0]] += table_content.replace(' ', '')

    while stack:
        old_level, old_data = stack.pop()
        data = old_data + data

    merged_dict = {}
    for small_dict in data:
        for key, value in small_dict.items():
            if key:
                merged_dict[key] = value
    return merged_dict

读取的时候 怎么删除超链接的 HYPERLINK这个信息

@Tiaohh 如果您需要在阅读时跳过 HYPERLINK 字段,或将 HYPERLINK 字段更改为文本,可以使用下面的代码作为示例,并将其放置在您需要的位置:

if node.node_type == aw.NodeType.FIELD_START:
    node = node.as_field_start()
    if node.field_type == aw.fields.FieldType.FIELD_HYPERLINK and "_Toc" not in node.get_field().result:
        node.get_field().unlink()

其中,node.get_field().unlink() 删除字段,只留下文本,或者使用node.get_field().remove() 删除字段和文本。 如果需要跳过 TOC 中的 HYPERLINK,则在本例中添加了 and "_Toc" not in node.get_field().result

言,受试者必须满足第\u0013 REF _Ref184816900 \r \h \* MERGEFORMAT \u00149.4.5\u0015节中概述的要求。这种怎么还存在呢

“7前言”: “社区获得性肺炎(CAP)是各年龄段人群中最常见的导致住院治疗和死亡的感染性疾病。流感和CAP是目前美国第八大导致死亡的原因\u0013 REF _Ref195302775 \r \h \* MERGEFORMAT \u00141\u0015。\n目前,我国缺少CAP年发病率和病死率的数据。据2013年中国卫生统计年鉴记载,2008年我国肺炎2周的患病率为1.1‰。25至39岁人群的死亡率为<1/100 000,65至69岁人群的死亡率为24/100 000,>85岁人群的死亡率高达864/100 000,死亡率随着年龄的增长而增加\u0013 REF _Ref195302795 \r \h \* MERGEFORMAT \u00142\u0015。通常,即使使用恰当的诊断方法,也有约51%的入院病例无法分离出特定的病原体。Jain等人进行的研究中,仅在一半的病例中发现了病原体,其中2/3是病毒。而肺炎链球菌是这项大型研究中分离到的最常见的细菌病原体。\n在过去的几十年中,已经观察到肺炎球菌对常用处方的β-内酰胺类抗生素的耐药性增加。同时,对红霉素和第一代四环素类抗生素的耐药也经常发生,这限制了重症患者抗生素的选择。因此,能够覆盖所有常见呼吸道病原体(包括非典型病原体)的氟喹诺酮类抗生素成为社区获得性细菌性肺炎(CABP)患者的首选药物。\n由于治疗CABP病原体的抗生素耐药性和安全性问题日益突出,对新抗生素的需求也与日俱增\u0013 REF _Ref195302932 \r \h \* MERGEFORMAT \u00143\u0015。包括多重耐药肺炎链球菌(MDRSP)在内的青霉素不敏感肺炎链球菌(PNSSP)和氨苄西林耐药流感嗜血杆菌\u0013 REF _Ref195302939 \r \h \* MERGEFORMAT \u00144\u0015,为CAP的治疗带来了挑战。这些细菌目前已被世界卫生组织纳入研究和开发新抗生素的优先病原体列表中\u0013 REF _Ref195302944 \r \h \* MERGEFORMAT \u00145\u0015。近期一项研究显示,从肺炎样本中分离的肺炎链球菌对青霉素、四环素、红霉素和甲氧苄啶磺胺甲恶唑的耐药率分别为12.7%、21.5%、45.6%和15.1%\u0013 REF _Ref195302949 \r \h \* MERGEFORMAT \u00146\u0015。虽然包括耐甲氧西林金黄色葡萄球菌(MRSA)在内的金黄色葡萄球菌仍是CABP的罕见致病原,但由于其可引起重症病例,在经验性治疗时需要纳入考量。在肺炎分离株中,金黄色葡萄球菌对几种抗生素的耐药率均较高(四环素3.4%,苯唑西林43.9%,红霉素54.5%,左氧氟沙星39.3%)\u0013 REF _Ref195302949 \r \h \* MERGEFORMAT \u00146\u0015。\n甲苯磺酸奥马环素(ZL-2401/PTK 0796)是一种新型四环素,为氨甲基环素类抗生素的第一个成员。其于2018年在美国获批用于治疗急性细菌性皮肤及皮肤结构感染(ABSSSI)和CABP,商品名为Nuzyra。同时基于甲苯磺酸奥马环素在中国开展的研究进一步证实了其有效性和耐受性,以上两个适应症于2021年同时在中国获批。\n甲苯磺酸奥马环素对大多数革兰阳性病原体(例如肺炎链球菌[包括MDRSP]和金黄色葡萄球菌[包括MRSA])具有体外活性。对非典型病原体(例如,嗜肺军团菌、肺炎支原体和肺炎衣原体)以及一些厌氧菌也有活性。重要的是,它同时具有抗流感嗜血杆菌(一种重要的革兰阴性病原体)的作用。甲苯磺酸奥马环素对表达四环素耐药机制(如外排、核糖体保护)的菌株以及对其他抗生素(包括甲氧西林、万古霉素、红霉素和环丙沙星)耐药的菌株均有活性\u0013 REF _Ref195302988 \r \h \* MERGEFORMAT \u00147\u0015。\n甲苯磺酸奥马环素已开发静脉(iv)和口服(po)两种剂型,并在两项大型全球3/3b期CABP临床研究(PTK0796-CABP-1200和PTK0796-CABP-19302)中与盐酸莫西沙星进行了比较。研究证实,甲苯磺酸奥马环素在整个研究期间治疗成人CABP的有效性不劣于盐酸莫西沙星,耐受特征(包括胃肠道副作用在内)与盐酸莫西沙星相当。\n本研究旨在满足中国卫生监管部门的上市后要求,进一步评估甲苯磺酸奥马环素(iv/po)和盐酸莫西沙星(iv/po)治疗中国成人CABP的安全性和有效性。\n”, 还有这种也需要过滤掉 只要文本

源性胸痛、心悸、快速性心律失常的AE或有其他临床指征(见第 HYPERLINK \l "_生命体征"\u0014 9.5.1.2.2 节)z这个没有删除成功

@Tiaohh 代码中指定了aw.fields.FieldType.FIELD_HYPERLINK,这就是为什么aw.fields.FieldType.FIELD_REF仍然存在于您的数据中。 您可以删除代码中的 node.field_type == aw.fields.FieldType.FIELD_HYPERLINK 字段,然后获取文档中的所有字段。