Repository navigation
FIX: Support tables, headers/footers, JSON templates, and async offload in WordDocConverter - #4835
Open
G.YASWANTH SAI (yaswanth2501) wants to merge 2 commits into
Conversation
…ad in WordDocConverter
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Tracking only integer cell IDs allows lxml proxy ID reuse, which can cause distinct cells to be skipped and their placeholders left unchanged.
1 open issue
What changed in this PR
Improves PyRIT’s WordDocConverter template handling and moves document processing off the event loop.
Changes:
- Expands placeholder replacement to tables, headers, footers, and all matching runs.
- Supports JSON template parameters with Python-literal fallback.
- Adds regression tests for document traversal, parsing, and asynchronous execution.
| File | Description |
|---|---|
tests/unit/converter/test_word_doc_converter.py |
Adds parsing, traversal, thread-offloading, and concurrent conversion tests. |
pyrit/converter/word_doc_converter.py |
Extends placeholder traversal, parses JSON parameters, and offloads document processing. |
Review effort: Balanced
Comment on lines
+282
to
+297
| def _iter_table_paragraphs(cls, *, tables: Any, seen_cells: set[int]) -> Iterator[Any]: | ||
| """Yield paragraphs across tables and nested tables, deduplicating merged cells.""" | ||
| for table in tables: | ||
| for row in table.rows: | ||
| for cell in row.cells: | ||
| cell_id = id(cell._tc) | ||
| if cell_id in seen_cells: | ||
| continue | ||
| seen_cells.add(cell_id) | ||
| yield from cell.paragraphs | ||
| yield from cls._iter_table_paragraphs(tables=cell.tables, seen_cells=seen_cells) | ||
|
|
||
| @classmethod | ||
| def _iter_document_paragraphs(cls, *, document: Any) -> Iterator[Any]: | ||
| """Yield paragraphs across the document body, tables, headers, and footers.""" | ||
| seen_cells: set[int] = set() |
There was a problem hiding this comment.
Updated _iter_table_paragraphs and _iter_document_paragraphs to store cell._tc and container._element directly in seen_cells and seen_containers rather than id(...), keeping lxml proxies alive throughout traversal.
Author
|
@microsoft-github-policy-service agree |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
WordDocConverter(pyrit/converter/word_doc_converter.py) had four defects when injecting placeholders into.docxtemplates, parsing JSON template parameters, and running inside async pipelines:_inject_into_existing_docxonly iterateddocument.paragraphs, silently skipping placeholders inside tables, nested tables, headers, and footers._replace_placeholder_in_paragraphreturnedTrueimmediately after updating the first matching run, leaving any subsequent matching runs in the same paragraph unreplaced._prepare_contentonly calledast.literal_eval(prompt)even though__init__documents JSON-encoded mappings, so valid JSON payloads containingtrue,false, ornullraisedValueError.convert_asyncran synchronouspython-docxfile reads, XML parsing, and serialization (_generate_new_docxand_inject_into_existing_docx) directly on the asyncio event loop.Changes:
_iter_table_paragraphsand_iter_document_paragraphsto traverse body paragraphs, tables/nested tables (deduplicating merged cells viacell._tc), and section headers/footers/tables (header,first_page_header,even_page_header,footer,first_page_footer,even_page_footer, deduplicating linked containers viacontainer._element)._replace_placeholder_in_paragraphto replace all matching runs in a paragraph before returning whether any replacement occurred._parse_template_mappingto parse withjson.loadsfirst and fall back toast.literal_eval, wrapping(ValueError, KeyError, TypeError)in_prepare_contentasValueError._generate_new_docxand_inject_into_existing_docxinconvert_asyncusingasyncio.to_thread(...), matchingPDFConverter.Before / after for JSON template input
{"name": "Alice", "active": true, "note": null}:Tests and Documentation
tests/unit/converter/test_word_doc_converter.pycovering multi-run paragraphs, JSONtrue/false/nullpayloads, non-dict / non-string-key rejection, tables/nested tables/merged cells, headers/footers (including linked sections),asyncio.to_threadworker-thread offloading, and concurrent end-to-end JSON +.docxinjection.uv run --frozen --no-sync -m pytest -q tests/unit/converter/test_word_doc_converter.py --cov=pyrit.converter.word_doc_converter --cov-report=term-missing: 36 passed, 100% statement coverage (132/132 statements).uv run --frozen --no-sync -m pytest -q tests/unit/converter/test_word_doc_converter.py tests/unit/backend/test_converter_service.py tests/unit/registry/test_converter_identifier_contract.py -k "word_doc or WordDocConverter": 40 passed, 100% diff coverage (47/47 lines).uv run --frozen --no-sync -m pytest -q tests/unit/converter/ tests/unit/backend/test_converter_service.py: 4,137 passed, 57 skipped.ruff check,ruff format --check,ty check,build_scripts.check_keyword_only_params, andbuild_scripts.check_public_imports: all passed.This change and its tests were prepared with AI assistance.