I have the attached docx file
input.docx (15.1 KB)
that has few bookmarks. The bookmarks have the following format “italxxxx”. I want to put each bookmarked text into a one cell table in the document. I am trying the code below:
def insert_table(doc, text1):
builder = aw.DocumentBuilder(doc)
table = builder.start_table()
builder.insert_cell()
builder.write(text1)
builder.end_row()
builder.end_table()
table_style = doc.styles.add(aw.StyleType.TABLE, "MyTableStyle1").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:
if node.node_type == aw.NodeType.RUN:
text_content += node.get_text()
# Insert the text content into the table
insert_table(doc, text_content)
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)
When I run this code I get this error:
Cannot add a style because a style with the same name already exists
Any help is appreciated.