Document Formatting changes after comparison

I am doing comparison between two docx files. The resultant files formatting changes a bit especially when it comes to lists or nested items.

original_content = await original.read()
modified_content = await modified.read()

original_doc = aw.Document(BytesIO(original_content))
original_doc.update_fields()
original_doc.accept_all_revisions()
modified_doc = aw.Document(BytesIO(modified_content))
modified_doc.update_fields()
modified_doc.accept_all_revisions()

compare_options = CompareOptions()
compare_options.ignore_formatting = True
compare_options.ignore_footnotes = True
compare_options.ignore_headers_and_footers = True

original_doc.compare(modified_doc, "Comparer", datetime.datetime.now(), compare_options)

output_stream = BytesIO()
original_doc.save(output_stream, aw.SaveFormat.DOCX)
output_stream.seek(0)

return StreamingResponse(output_stream, media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document")

@LikithaThatikonda

When comparing two DOCX files using Aspose.Words, it is common to encounter formatting changes, particularly with lists or nested items. To minimize these formatting issues during the comparison, you can utilize the CompareOptions class to ignore formatting differences.

In your code, you have already set compare_options.ignore_formatting = True, which is a good approach. This setting ensures that the comparison focuses solely on the textual content, disregarding any formatting changes.

If you still notice formatting discrepancies, consider the following:

  1. Review Other Options: Ensure that other relevant options, such as ignore_headers_and_footers and ignore_footnotes, are configured according to your needs.

  2. Granularity: Adjusting the granularity of the comparison may yield better results, especially for nested lists. For instance, comparing at the word level can enhance accuracy.

Here’s an example of how to set the granularity:

compare_options.granularity = aw.Granularity.WordLevel  # Compare at word level

By focusing on the content rather than formatting, you should achieve a more accurate comparison result.

If you continue to experience issues, please provide more details about the specific formatting changes you are observing.

@LikithaThatikonda Could you please attach the problematic input documents here for testing? We will check the issue and provide you more information.