How moon-like-gray-cat/wordformatter works
Purpose and Entry Points
WordFormatter is a Windows-oriented desktop utility that normalizes .docx formatting: it detects headings, applies configured typography and Word heading styles, standardizes paragraph indentation/alignment, normalizes parentheses, and centers figure/table captions.
Primary launch path is run.py, which starts the Tkinter application in src/wordtool/app/main.py. The package is organized as a GUI shell over a document-formatting core.
Desktop Application Layer
The UI is built with Tkinter and presents document selection plus user-controlled heading patterns, fonts, sizes, boldness, and processing options. Layout and widgets reside in src/wordtool/app/ui_components.py; application startup/window coordination is in src/wordtool/app/main.py.
UI defaults or persisted interface settings are represented by src/wordtool/resources/ui_config.json, with shared application configuration in src/wordtool/config.py.
Event-to-Processing Boundary
User actions are handled in src/wordtool/app/event_handlers.py. This is the key boundary between GUI state and document processing: it gathers selected input/options, invokes the formatter, and reports completion or failure back to the desktop UI.
The formatter operates on .docx; .doc is explicitly outside the supported input boundary. Processing failures may result in an output file being announced but not successfully written, so handler-level error reporting is material.
Document Formatting Core
src/wordtool/core/formatter.py contains the primary business pipeline. It interprets paragraph content and applies Word document mutations rather than exposing a separate service/API layer.
Core behavior includes:
• Detecting configured textual numbering patterns such as Chinese numerals, hierarchical Arabic numbering, letters, Roman numerals, and circled numbers.
• Mapping recognized levels to Word’s built-in heading styles so generated documents can support tables of contents.
• Applying per-level font, size, and bold settings to headings and body text.
• Normalizing paragraph layout: headings left aligned; paragraphs flush-left with a two-character first-line indent.
• Replacing ASCII ( with Chinese full-width ( and centering table/figure captions.
Heading Recognition and Numbering Dependencies
Heading classification is prefix-based and paragraph-oriented: numbering must be at the beginning of a hard-return-delimited paragraph. This makes paragraph boundaries an important input contract and means ordinary body text beginning with a configured marker can be misclassified.
The tool can also process Word automatic numbering when the user enables that option. That capability depends on Microsoft Office being installed; WPS alone is insufficient for automatic-list-number recognition. Text-number recognition and the remaining formatting features do not share that Office dependency.
Output and Delivery
For an input such as xxx.docx, processing writes a formatted Word document in the chosen directory, conventionally named 格式化_xxx.docx. The tool is intended for Windows 7+ distribution as an executable rather than as a hosted application.
Packaging is supported by scripts/build_exe.bat; project metadata/dependencies are declared in pyproject.toml and requirements.txt. There is no server runtime, database, or network service in the repository’s described architecture.
Main entry points: formatter.py Document formatter · event_handlers.py Processing handlers · ui_components.py Document options UI · main.py Window coordinator
Components 18
Desktop application
- Application launcher · Starts the Tkinter application.
run.py - Window coordinator
src/wordtool/app/main.py - Document options UI · Collects files, typography, heading patterns, and processing options.
src/wordtool/app/ui_components.py - UI defaults
src/wordtool/resources/ui_config.json - Shared configuration
src/wordtool/config.py - Processing handlers · Invokes formatting and reports write failures to the UI.
src/wordtool/app/event_handlers.py
Formatting core
- Document formatter · Mutates Word paragraphs and styles using selected options.
src/wordtool/core/formatter.py - Paragraph boundaries · Headings must begin a hard-return-delimited paragraph.
- Heading recognition · Recognizes configured Chinese, Arabic, letter, Roman, and circled numbering.
src/wordtool/core/formatter.py - Automatic numbering · Recognition requires Microsoft Office; WPS alone is insufficient.
src/wordtool/core/formatter.py - Heading style mapping · Maps detected levels to built-in Word heading styles.
src/wordtool/core/formatter.py - Paragraph normalization · Applies typography, alignment, indentation, parentheses, and caption rules.
src/wordtool/core/formatter.py
Build and delivery
- Executable build
scripts/build_exe.bat - Dependencies and metadata
pyproject.toml - Runtime dependencies
requirements.txt - Windows executable · Targeted at Windows 7+.
Other components
- Input .docx · Only .docx is supported; .doc is outside the input boundary.
- Formatted .docx · Conventionally written as 格式化_xxx.docx in the chosen directory.
Connections 20
- Application launcher → Window coordinator: starts
- Window coordinator → Document options UI: renders
- UI defaults → Document options UI: supplies defaults
- Shared configuration → Window coordinator: configures
- Document options UI → Processing handlers: user actions
- Processing handlers → Input .docx: reads selected file
- Processing handlers → Document formatter: passes options and input
- Document formatter → Paragraph boundaries: relies on
- Document formatter → Heading recognition: classifies paragraphs
- Automatic numbering → Heading recognition: optional numbering source
- Heading recognition → Heading style mapping: recognized level
- Heading style mapping → Paragraph normalization: styles headings
- Document formatter → Paragraph normalization: applies formatting
- Paragraph normalization → Formatted .docx: writes
- Document formatter → Formatted .docx: processing result
- Processing handlers → Document options UI: completion or failure
- Dependencies and metadata → Executable build: build inputs
- Runtime dependencies → Executable build: dependencies
- Executable build → Windows executable: packages
- Windows executable → Application launcher: distributes