Skip to content

Inherit {{p }} paragraph style onto Subdoc bodies - #668

Open
alranel wants to merge 1 commit into
elapouya:masterfrom
alranel:subdoc-p-style
Open

alranel wants to merge 1 commit into
elapouya:masterfrom
alranel:subdoc-p-style

Conversation

@alranel

@alranel alranel commented Oct 14, 2026

Copy link
Copy Markdown

Problem

When a template uses {{p var }}, patch_xml strips the enclosing <w:p>
(including w:pStyle / alignment) before Jinja runs. Body paragraphs in a
Subdoc therefore ignore the style the author applied to the placeholder line.

Proposal

Before patch_xml removes {{p var }} paragraphs, scan for style/alignment.
At render() time, apply those to plain body paragraphs of any Subdoc in the
context keyed by that variable name (Heading / List / Title / Subtitle left
unchanged). First occurrence wins.

Works for programmatic new_subdoc() and for Subdocs created by other means
(e.g. JSON CLI envelopes from a separate PR).

Implementation

  • New docxtpl.p_style (scan_p_placeholder_styles, apply_p_placeholder_styles)
  • Hooked at the start of DocxTemplate.render(), before build_xml / patch_xml
  • Re-export scan_p_placeholder_styles from docxtpl

How to try

cd tests
python subdoc_p_style.py

Scan {{p var }} placeholder style and alignment before patch_xml and
apply them to plain Subdoc body paragraphs during render().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant