Converting Word Lists to Text

@ln22 You can use the following code to unlink only LISTNUM fields:

for f in doc.range.fields:
    if f.type == aw.fields.FieldType.FIELD_LIST_NUM:
        f.unlink()

If I run the following code below on the specific paragraph range.fields the (CC) sadly changes to (A).

    for p in aspose_doc.get_child_nodes(aw.NodeType.PARAGRAPH, True):
        para = p.as_paragraph()
        if para.is_list_item and para.list_label.label_string != "":
            if para.list_label.label_string == "(CC)":
                range_text = para.range.text
                run_0 = para.runs[0]
                for f in para.range.fields:
                    if f.type == aw.fields.FieldType.FIELD_LIST_NUM:
                        f.unlink()

Also, the code above then leads to list below the unlinked list to remove it from the list and start one level up. For example, if I unlink an “(ii) (A)” then the next list label which should be ‘(iii)’ becomes ‘(ii)’. Is there a way to relink?

It seems f.display_result holds the listnum actual text. Can this be inserted into the paragraph text in the proper place somehow?

@ln22 You can create a Run with this text and insert it after the field. Something like this:

for f in para.range.fields:
    if f.type == aw.fields.FieldType.FIELD_LIST_NUM:
        para.insert_after(aw.Run(doc, f.display_result), f.end)

That code works accept it clears the styling if the f.display_result text. How could I make the run that same style as the current run? For example if f.display_result=‘(a)’ is italic, the current code removes this italic styling, and I would prefer is stay with the original styling.

@ln22 You can clone the run from the first value to keep the formatting:

for f in para.range.fields:
    if f.type == aw.fields.FieldType.FIELD_LIST_NUM:
        r =  f.end.previous_sibling.clone(False).as_run()
        r.text = f.display_result
        para.insert_after(r, f.end)

Great! How could I do the same for this code:

fake_list_label_run = aw.Run(aspose_doc, label)
para.list_format.remove_numbers()
para.prepend_child(fake_list_label_run)

@ln22 Sure, you can modify the code like this:

fake_list_label_run = para.runs[0].clone(False).as_run()
fake_list_label_run.text = label
para.list_format.remove_numbers()

Hello @alexey.noskov ,

I when I run the following code:

r =  f.end.previous_sibling.clone(False).as_run()

I sometimes get the following error:
RuntimeError: Proxy error(InvalidCastException): Unable to cast object of type 'Aspose.Words.BookmarkStart' to type 'Aspose.Words.Run'.

Do you know why this would occur?

@ln22 The problem occurs because the previous sibling of field end is BookmarkStart. Please modify the code like this:

r = f.end.previous_sibling
while r != None and r.node_type != aw.NodeType.RUN :
    r = r.previous_sibling
r =  r.clone(False).as_run()

Thanks this works great!

1 Like