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.