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.
importosfromgroupdocs.editorimportEditor,EditableDocument,Licensefromgroupdocs.editor.formatsimportWordProcessingFormatsfromgroupdocs.editor.optionsimportWordProcessingSaveOptionsdefoptimize_memory_usage():# Optionally set a licenselicense_path=os.path.abspath("./GroupDocs.Editor.lic")ifos.path.exists(license_path):License().set_license(license_path)withEditor("./sample-document.docx")aseditor:original=editor.edit()modified=EditableDocument.from_markup(original.get_embedded_html())# Enable memory optimization for the saving processsave_options=WordProcessingSaveOptions(WordProcessingFormats.DOCX)save_options.optimize_memory_usage=Trueeditor.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.