Asset loaders and direct images
Resolve logical asset IDs to bytes and insert BytesIO images into cells.
Compare input and output
Compare the actual HTML/MathML input in your browser with the generated DOCX exported to PDF by LibreOffice. Open the DOCX in Word to inspect editable equations. This example also builds content directly in Word. The browser panel shows only the inserted HTML/MathML fragments, in call order; the PDF includes the surrounding Word document and its styles.
Browser · HTML / MathML
Open HTML previewLibreOffice · PDF
Open PDFFor Markdown and LaTeX examples, the browser shows the generated HTML or MathML. Browser MathML support, Word styles, fonts and page layout can produce different results.
The PDF panel shows page images. Open the original PDF to zoom, search or select text, or download the DOCX to edit it.
Run
Extract the example bundle, install the example dependencies, and run this command from the folder containing examples/:
python -m examples.12_asset_loader
Complete source
"""12 asset loader example."""
from io import BytesIO
from mathml2docx import MathmlToDocx
from .support import ROOT, save
# Resolve allowed logical asset IDs. No network access or arbitrary file paths.
assets = {"asset:logo": (ROOT / "assets/swatch.png").read_bytes()}
parser = MathmlToDocx(image_loader=assets.__getitem__)
document = parser.parse_html_string(
'<p>Asset <img src="asset:logo" width="80"> inline</p>'
)
cell = document.add_table(1, 1).cell(0, 0)
parser.add_image_to_cell(cell, BytesIO(assets["asset:logo"]))
save(document, "12_asset_loader.docx")