Memory optimization option

On this page

By default GroupDocs.Editor tries to perform computations and complete the task as fast as possible, and if this challenge requires a lot of memory to be used, GroupDocs.Editor does it. However, in some very specific cases, when the processing document is very huge, or the user machine has a very limited amount of free memory, an out-of-memory error may occur. In order to solve such a problem the WordProcessingSaveOptions class contains the optimize_memory_usage property:

optimize_memory_usage  # bool

By default it has a False value, which means that the memory optimization is disabled for the sake of the best possible performance. By setting it to True the user can enable another document generating mechanism, which can significantly decrease memory consumption while generating large documents at the cost of slower generation time while performing the editor.save() method.

Complete example

The example below loads the sample document, edits it, and saves the result to DOCX with the optimize_memory_usage flag enabled.

import os
from groupdocs.editor import Editor, EditableDocument, License
from groupdocs.editor.formats import WordProcessingFormats
from groupdocs.editor.options import WordProcessingSaveOptions

def optimize_memory_usage():
    # Optionally set a license
    license_path = os.path.abspath("./GroupDocs.Editor.lic")
    if os.path.exists(license_path):
        License().set_license(license_path)

    with Editor("./sample-document.docx") as editor:
        original = editor.edit()
        modified = EditableDocument.from_markup(original.get_embedded_html())

        # Enable memory optimization for the saving process
        save_options = WordProcessingSaveOptions(WordProcessingFormats.DOCX)
        save_options.optimize_memory_usage = True

        editor.save(modified, "./optimized-document.docx", save_options)
        print("Saved the edited document with memory optimization enabled")

        original.dispose()
        modified.dispose()

if __name__ == "__main__":
    optimize_memory_usage()

sample-document.docx is the sample file used in this example. Click here to download it.

Binary file (DOCX, 49 KB)

Download full output

On this page