md2star.postprocess module

postprocess.py — Re-inject Pandoc-stripped styles into the output DOCX.

Pandoc’s DOCX writer reads the reference template’s word/styles.xml but rewrites it from its own catalogue on output. Paragraph and character styles defined in the reference that Pandoc happens to use survive; arbitrary custom-styled table styles do not — Pandoc drops them, leaving the <w:tblStyle w:val="X"/> references in the body pointing at nothing.

This module reopens the produced .docx zip, locates word/styles.xml, and inserts the MyTable and MyTableSmall table styles (thin gray #9E9E9E borders; the small variant also forces an 8 pt font + tighter cell margins). It is idempotent: existing definitions are left untouched.

The XML is parsed with xml.etree.ElementTree rather than mutated as a string, so trailing whitespace, attribute ordering, or namespace prefix differences cannot break the rewrite.

Invoked by the md2docx CLI immediately after Pandoc finishes.

Author

[Warith HARCHAOUI](https://linkedin.com/in/warith-harchaoui/)

md2star.postprocess.center_standalone_images(docx_path)[source]

Centre standalone (non-table) images in docx_path; True if changed.

md2star already sizes each bare image to a contain-fit that never exceeds the page (see md2star.preprocessing.images.image_size_attr()); Pandoc, however, leaves the image left-aligned. This adds the missing centring so an image outside a table sits centred at the largest size that still fits the page — in the DOCX and, via LibreOffice, in the PDF. Table-cell images are untouched.

Parameters:

docx_path (str)

Return type:

bool

md2star.postprocess.inject_table_styles(docx_path)[source]

Inject MyTable + MyTableSmall into docx_path’s word/styles.xml.

Returns True if any style was added, False if both were already present (or if the styles.xml could not be parsed — defensive no-op).

Parameters:

docx_path (str)

Return type:

bool

md2star.postprocess.strip_table_normal_for_pdf(docx_path)[source]

Neutralize the bundled template’s TableNormal0 in docx_path.

Word renders the style fine; LibreOffice headless does not. We call this on the intermediate DOCX produced inside the md2pdf pipeline so the soffice render is correct. The user’s DOCX output (when they call md2docx) is untouched.

Returns True if any mutation happened, False otherwise.

Parameters:

docx_path (str)

Return type:

bool