How mcs391/word-bid-beautifier works
1. Repository purpose
- This is a Python utility repo for post-processing Word .docx bid documents used in HIK/华数-style tender templates.
- The architecture is centered on direct OOXML manipulation, not on Word automation or a GUI.
2. Main executable subsystem
- scripts/bid_doc_optimizer.py is the primary entry point.
- It exposes a command-line workflow that takes an input .docx, applies selected phases, and writes a new .docx.
- This is the script that should be treated as the system’s “orchestrator” in an architecture diagram.
3. Secondary utility subsystem
- scripts/word_bid_beautify.py is a supporting script focused on style beautification.
- The README positions it as a narrower tool compared with the main optimizer, so it should be drawn as a helper/legacy/specialized path rather than the core pipeline.
4. Pipeline structure
- The main tool is organized as a three-phase pipeline:
- Phase 0: fix numbering inheritance and list linkage at the OOXML level.
- Phase 1: repair broken chapter/heading numbering sequences and remove stray textual prefixes.
- Phase 2: apply style cleanup and layout polishing.
- In a diagram, these phases form a linear processing chain, though each phase can also be run independently via CLI flags.
5. Phase 0 architectural boundary
- Phase 0 is the most structural step: it modifies the internal Word model so headings inherit numbering correctly.
- It touches three OOXML areas:
- word/numbering.xml for list definitions,
- word/styles.xml for style-to-list binding consistency,
- word/document.xml for paragraph-level numbering references.
- This phase exists to fix Word’s behavior when users manually apply H2/H3/H4 styles.
6. Phase 1 and Phase 2 responsibilities
- Phase 1 is logic-level normalization of heading numbers and hardcoded prefixes in text.
- Phase 2 is presentation-level refinement: heading styles, indentation, font family normalization, spacing adjustments, and repeated numbering cleanup.
- The distinction matters: Phase 1 corrects content structure, while Phase 2 corrects appearance.
7. Document processing data flow
- The repository’s data flow is:
- read .docx archive,
- extract relevant OOXML parts,
- parse and transform XML/text content,
- repackage the archive as a new .docx.
- This means the system operates on the file as a structured package, not as a rendered document.
8. Core file-level dependencies
- The tool’s architectural dependency set is intentionally small:
- Python 3.8+,
- standard library only,
- Word OOXML package structure.
- There are no external APIs, databases, or runtime services involved.
9. Template and style model
- The repository is specialized around a fixed HIK style mapping:
- styles 12–16 correspond to chapter through fifth-level headings,
- style 17 is body text.
- A diagram should show this as a template-specific domain model, because the tool is not generic for arbitrary Word documents.
10. Input/output boundary
- The main architectural boundary is the .docx file itself.
- Input is a bid document with existing OOXML definitions; output is a new, modified .docx.
- The README explicitly notes that the tool assumes the source document already contains the expected multi-level list structure in many cases.
11. Configuration and operational controls
- The CLI flags act as the system’s configuration layer:
- phase selection,
- chapter range bounds,
- indentation values,
- dry-run mode,
- skip-prefix-stripping option.
- These controls change how the transformer behaves without changing the underlying architecture.
12. Reference and documentation subsystem
- references/ooxml-quickref.md serves as the internal OOXML reference for developers.
- README.md documents usage and architecture; SKILL.md suggests the repo is also packaged for a workflow/assistant environment.
- These are supporting artifacts, not runtime components.
13. Operational concerns
- The tool includes validation such as paragraph-count consistency checks after processing.
- It is designed for safe batch document cleanup, with the README emphasizing backup before use.
- In a diagram, this can be represented as a post-transform validation step before output.
14. What is not in scope
- There is no web frontend, server process, or background job system in this repository.
- There is also no persistent state: each run is a stateless file transformation job.
15. Suggested architecture diagram framing
- Draw one main pipeline box labeled bid_doc_optimizer.py.
- Feed it a .docx input package, pass it through three sequential transformation phases over OOXML parts, then emit a new .docx.
- Show word/document.xml, word/styles.xml, and word/numbering.xml as the main internal data surfaces, with word_bid_beautify.py as a supporting utility and references/ooxml-quickref.md as developer documentation.
Main entry points: bid_doc_optimizer.py Optimizer · word_bid_beautify.py Beautifier · README.md README · SKILL.md SKILL
Components 17
Runtime pipeline
Command-line document transformation workflow and its specialized helper path.
- DOCX input · Source bid document archive provided to the tool.
- Optimizer · Primary command-line entry point that coordinates the document pipeline.
scripts/bid_doc_optimizer.py - CLI flags · Phase selection, ranges, indentation, dry-run, and prefix control.
scripts/bid_doc_optimizer.py - Phase 0 · Repairs numbering inheritance and style-list linkage at the package level.
scripts/bid_doc_optimizer.py - Phase 1 · Normalizes chapter numbering and removes stray textual prefixes.
scripts/bid_doc_optimizer.py - Phase 2 · Applies style cleanup, indentation, spacing, and repeated-numbering cleanup.
scripts/bid_doc_optimizer.py - Validation · Post-transform checks such as paragraph-count consistency before writeout.
scripts/bid_doc_optimizer.py - DOCX output · Repackaged modified document emitted by the optimizer.
- Beautifier · Specialized helper script for style-focused document cleanup.
scripts/word_bid_beautify.py
OOXML surfaces
Word package parts that are read and rewritten during processing.
- OOXML package · The zipped Word document container from which relevant parts are extracted and rebuilt.
- document.xml · Paragraph content and numbering references within the Word document body.
- styles.xml · Style definitions and style-to-list bindings used by HIK templates.
- numbering.xml · Numbering and multilevel list definitions that drive heading inheritance.
- HIK styles · Fixed style mapping where 12–16 map to chapter headings and 17 to body text.
Support files
Documentation and reference artifacts used by developers and operators.
Connections 27
- DOCX input → Optimizer: open · The CLI consumes the source .docx package.
- Optimizer → CLI flags: reads · Operational flags control which phases and bounds run.
- Optimizer → Phase 0: runs · Phase 0 can execute as part of the main pipeline.
- Phase 0 → document.xml: updates · Paragraph-level numbering references are repaired.
- Phase 0 → styles.xml: updates · Style-to-list binding consistency is corrected.
- Phase 0 → numbering.xml: updates · List definitions and inheritance behavior are fixed.
- Phase 0 → Phase 1: feeds · Structural fixes enable later numbering normalization.
- Optimizer → Phase 1: runs · Phase 1 normalizes heading numbering and prefixes.
- Phase 1 → document.xml: reads/writes · Heading text and numbering sequences are normalized.
- Phase 1 → Phase 2: feeds · Content cleanup flows into presentation polishing.
- Optimizer → Phase 2: runs · Phase 2 performs style and layout cleanup.
- Phase 2 → styles.xml: updates · Heading styles, fonts, indentation, and spacing are refined.
- Phase 2 → document.xml: updates · Paragraph layout and repeated numbering cleanup are applied.
- Optimizer → Validation: checks · Validation runs after transformation before output is written.
- Validation → DOCX output: gate · Only validated output is repackaged.
- Optimizer → DOCX output: write · A new modified .docx is emitted.
- OOXML package → document.xml: contains · The archive is unpacked into document XML parts.
- OOXML package → styles.xml: contains · The archive carries style definitions.
- OOXML package → numbering.xml: contains · The archive carries numbering definitions.
- Optimizer → OOXML package: extracts/rebuilds · Processing operates on the OOXML package boundary.
- Beautifier → styles.xml: focuses on · The helper tool concentrates on style beautification.
- Beautifier → document.xml: polishes · It applies narrower text and layout cleanup.
- HIK styles → styles.xml: defines · Template-specific style semantics are expressed through styles.xml.
- HIK styles → Phase 1: constrains · Heading normalization follows the HIK numbering model.
- README → Optimizer: documents · README describes the main pipeline and usage.
- OOXML ref → Optimizer: supports · OOXML reference guides implementation details.
- SKILL → README: packages · Workflow metadata accompanies the repository docs.