Bookmarks seem corrupted after using insert_document

We use aspose.words for python to load a Document object, search for text and then insert bookmarks at found locations. Then we iterate over the bookmarks to insert additional formatted content using “insert_document”.

Once complete, we wish to remove the original bookmarks. However, while code like ‘bm = doc.range.bookmarks.get_by_name(“name”)’ finds the bookmark, subsequently using ‘bm.remove()’ then fails with ‘Cannot find bookmark “name” in the document’.

As a simple test, we removed the actual insertion of the new content and the cycle works well, adding and removing bookmarks as expected. Note that the inserted content does not contain any “bookmark” data.

Thanks for any help!

@bkeane

It appears that the issue with bookmarks becoming corrupted after using insert_document may stem from modifications to the document structure during the insertion process. This can affect the positions or references of the bookmarks within the document.

Here are some suggestions to troubleshoot and potentially resolve the issue:

  1. Check Bookmark References: After inserting the document, verify that the bookmarks are still valid. You can do this by checking if the bookmark’s range is still valid before attempting to remove it.

  2. Remove Bookmarks by Collection: Instead of directly calling bm.remove(), consider removing the bookmark by accessing the Document’s bookmarks collection and removing it by name or index. For example:

    doc.range.bookmarks.remove("name")
    
  3. Revalidate Bookmarks: After the document insertion, re-fetch the bookmarks to ensure you are working with the latest state of the document. This can help if the bookmarks have been altered or moved.

  4. Debugging: If the problem persists, log the state of the bookmarks before and after the insertion to identify any changes that might be causing the issue.

If these suggestions do not resolve the problem, you may need to explore additional resources or community forums for more specific guidance related to your use case.

Tried a few things already:

  • doc.range.bookmarks.remove(“name”)
  • bookmarkobject.remove()
  • caching bookmark objects before doing content insertion then using cache to “.remove()”
  • bookmark name with leading “_”, etc.

Is there perhaps any way to “refresh” the Document structure and rescan for bookmarks? Or anything we should add to the inserted content to avoid corrupting the bookmarks?

Thanks!

@bkeane Could you please attach your problematic input documents and simple code that will allow us to reproduce the problem? We will check the issue and provide you more information.