Issue removing bookmarked text

I am working on the following document
input.docx (20.4 KB)

. I am trying to put each bookmarked text in a single cell table together with its bookmark. The following code fails while trying to delete the bookmarked text
(bookmark.text="").

def insert_table(doc, text1,bkmark):
    builder = aw.DocumentBuilder(doc)
    # move to the bookmarked text
    builder.move_to_bookmark(bkmark)
    table = builder.start_table()
    builder.insert_cell()

    # delete the bookmarked text
    bookmark = doc.range.bookmarks.get_by_name(bkmark)
    bookmark.text=""

#  remove the bookmark
    doc.range.bookmarks.remove(bkmark)

    #I create the same bookmark inside the table cell
    builder.start_bookmark(bkmark)
    builder.write(text1)
    builder.end_bookmark(bkmark)
    builder.end_row()
    builder.end_table()

    style_name = "MyTableStyle1"
    style = doc.styles.get_by_name(style_name)
    if style == None :
        style = doc.styles.add(aw.StyleType.TABLE, style_name)
    table_style = style.as_table_style()
    table_style.conditional_styles.first_row.shading.background_pattern_color = drawing.Color.green_yellow
    table_style.conditional_styles.first_row.shading.texture = aw.TextureIndex.TEXTURE_NONE

    table.style = table_style

    return doc

def extract_content_from_bookmarks(doc):
    extracted_content = []
    for bk in doc.range.bookmarks:
        bookmark_name = bk.name
        if "ital" in bookmark_name:
            extracted_nodes_exclusive = ExtractContentHelper.extract_content(bk.bookmark_start, bk.bookmark_end, False)
            # Get the text content from the extracted nodes
            text_content = ""
            for node in extracted_nodes_exclusive:
                text_content += node.get_text()
           #  Insert the text content into the table
            insert_table(doc, text_content,bookmark_name)

    try:
        doc.save("table_formatting.docx")
        print("Document saved successfully.")
    except Exception as e:
        print("Error occurred while saving the document:", e)

# Example usage
doc = aw.Document("input.docx")
extract_content_from_bookmarks(doc)

Any help is appreciated.

@ansar2024 I think you should better use the extracted content not only the text. Also, bookmarks collection is a leave collection so the loop might go into infinite loop. Please try using the following modified code:

def insert_table(doc, content, bkmark):
    print(bkmark)
    builder = aw.DocumentBuilder(doc)
    # move to the bookmarked text
    builder.move_to_bookmark(bkmark, False, True)

    # delete the bookmarked text
    bookmark = doc.range.bookmarks.get_by_name(bkmark)
    bookmark.text=""

    # remove the bookmark
    bookmark.remove()

    table = builder.start_table()
    builder.insert_cell()
   
    # Insert content
    builder.insert_document(content, aw.ImportFormatMode.USE_DESTINATION_STYLES)

    builder.end_row()
    builder.end_table()

    style_name = "MyTableStyle1"
    style = doc.styles.get_by_name(style_name)
    if style == None :
        style = doc.styles.add(aw.StyleType.TABLE, style_name)
    table_style = style.as_table_style()
    table_style.conditional_styles.first_row.shading.background_pattern_color = pydraw.Color.green_yellow
    table_style.conditional_styles.first_row.shading.texture = aw.TextureIndex.TEXTURE_NONE

    table.style = table_style

    return doc

def extract_content_from_bookmarks(doc):

    # Get the bookmarks which shhould be processed
    bookmakrs = []
    for bk in doc.range.bookmarks:
        bookmark_name = bk.name
        if "ital" in bookmark_name:
            bookmakrs.append(bookmark_name)

    extracted_content = []
    for bookmark_name in bookmakrs:
        bk = doc.range.bookmarks.get_by_name(bookmark_name)
        extracted_nodes_inclusive = ExtractContentHelper.extract_content(bk.bookmark_start, bk.bookmark_end, True)
        # Insert the text content into the table
        insert_table(doc, ExtractContentHelper.generate_document(doc, extracted_nodes_inclusive), bookmark_name)

    try:
        doc.save("table_formatting.docx")
        print("Document saved successfully.")
    except Exception as e:
        print("Error occurred while saving the document:", e)

# Example usage
doc = aw.Document("C:\\Temp\\input.docx")
extract_content_from_bookmarks(doc)
doc.save("C:\\Temp\\out.docx")
1 Like

Thank you very much for your support. It works fine.

1 Like