1、替换表格内容 2、需要在表格第二行插入内容

需要替换Subject Demographics,Disease Background:,Dosing/Disposition Information:的表格内容

需要在Sequence of Events,Investigator Causality Statement:的表格第二行插入内容

测试2024.docx (12.9 KB)

@hhh1111

请提供更多关于您希望如何替换和插入表格内容的详细信息,例如您希望替换的具体内容和要插入的内容是什么?

Other Reasons for Narratives:

Subject Demographics:
ffsfsfsfsdfs

Disease Background:
hfhfhfhfghgf

Dosing/Disposition Information:
grgdrgfsgsgsgrs

需要替换Subject Demographics,Disease Background:,Dosing/Disposition Information:的表格内容

@vyacheslav.deryushev

@hhh1111 如果我理解正确,你需要从第一张表中找到内容,然后把它放在第二张表中。您可以使用以下代码作为示例来查找和放置内容:

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

paragraphs = doc.get_child_nodes(aw.NodeType.PARAGRAPH, True)

for paragraph in paragraphs:
    paragraph = paragraph.as_paragraph()

    if "Subject Demographics:" in paragraph.get_text():
        row = paragraph.get_ancestor(aw.NodeType.ROW)
        cell = self.get_next_row_cell(row)
        content = cell.get_text()

    if "Sequence of Events:" in paragraph.get_text():
        row = paragraph.get_ancestor(aw.NodeType.ROW)
        cell = self.get_next_row_cell(row)
        cell.remove_all_children()
        cell.ensure_minimum()
        cell.first_paragraph.append_child(aw.Run(doc, content))

doc.save("output.docx")

def get_next_row_cell(self, row):
    row = row.as_row()
    rowIdx = row.parent_table.rows.index_of(row)

    cell = row.parent_table.rows[rowIdx + 1].first_cell
    cell = cell.as_cell()

    return cell

不对我需要替换里面的内容

替换表格里面内容ffsfsfsfsdfs 把它替换成别的内容

比如 Subject Demographics:找到它 然后把它下面的表格数据替换成别的数据

1、还有一个问题怎么删除这里的内容。并且需要保留一个换行符


2、Subject Demographics:找到它 然后把它下面的表格数据替换成别的数据

@hhh1111

  1. 如果你想删除一个表,你可以使用文本“叙述的其他原因:”和paragraph.get_ancestor(aw.NodeType.TABLE)进行搜索,然后删除该表。如果需要删除单元格的内容,请使用上面的代码:
row = paragraph.get_ancestor(aw.NodeType.ROW)
cell = self.get_next_row_cell(row)
cell.remove_all_children()
cell.ensure_minimum()
  1. 如果你知道单元格中的文本,你可以使用doc.range.replace(…(doc.range.replace_regex(…).
    或者,您可以使用上述代码来实现此目的:
if "Subject Demographics:" in paragraph.get_text():
    row = paragraph.get_ancestor(aw.NodeType.ROW)
    cell = self.get_next_row_cell(row)
    cell.remove_all_children()
    cell.ensure_minimum()
    cell.first_paragraph.append_child(aw.Run(doc, content))