This article shows and explains advanced techniques and approaches while working with EditableDocument, including saving its HTML markup and the accompanying resources.
Introduction
The EditableDocument class is one of the most important among all types in GroupDocs.Editor. It is focused on producing HTML markup and saving it together with its resources, and it exposes several ways to do so. This article covers them.
Saving the HTML markup to a file
The simplest way to persist an EditableDocument is its save() method. When invoked with only the HTML file path, it writes the HTML markup, and the accompanying resources are placed next to it automatically.
fromgroupdocs.editorimportEditorwithEditor("document.docx")aseditor:editable=editor.edit()# Write the HTML markup to a fileeditable.save("document.html")
Obtaining the HTML markup as a string
When the markup is needed in memory rather than on disk — for example, to push it into a stream, a database, or an HTTP response — it can be obtained as a string with get_content(), get_body_content(), or the fully self-contained get_embedded_html(). The resulting string can then be written into any binary stream, such as an io.BytesIO:
importiofromgroupdocs.editorimportEditorwithEditor("document.docx")aseditor:editable=editor.edit()# All resources are embedded inside a single self-contained stringembedded_html=editable.get_embedded_html()# Write the markup into an in-memory binary streamstream=io.BytesIO()stream.write(embedded_html.encode("utf-8"))stream.seek(0)
When get_embedded_html() is used, the produced string is fully autonomous: stylesheets are embedded into the markup and images are serialized with base64 encoding directly into the src attributes, so no external resources are required.
Complete code example
The example below loads a document, opens it for editing, and saves its HTML markup to a file.
importosfromgroupdocs.editorimportEditor,Licensedefsaving_editabledocument_to_stream():# 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:editable=editor.edit()# Save the HTML markup to a fileeditable.save("output.html")# The same markup is also available in memory as a stringembedded_html=editable.get_embedded_html()print("Embedded HTML length:",len(embedded_html))editable.dispose()if__name__=="__main__":saving_editabledocument_to_stream()
sample-document.docx is the sample file used in this example. Click here to download it.