# iivanyaa/staticapp architecture

> Architecture diagram of the GitHub repository iivanyaa/staticapp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/iivanyaa/staticapp
- Repository: https://github.com/iivanyaa/staticapp

## Overview

#### Purpose and Entry Points
A local Python document-processing pipeline converts scanned Russian statistical-card PDFs into normalized Excel records and then populates DBF files. Primary CLI entry points are `aligner_tool.py` for PDF→Excel and the DBF export implementation represented by `dbf_debugger_refactored.py` / related DBF modules. A desktop UI may be provided by `gui.py`; packaged Windows output is `dist/STATCARD_PROCESSOR.exe`.

#### Input, Templates, and Calibration
PDFs are staged in `SCARD/`; card/page-type reference material is under `EtalonPDFCards/` and template assets are expected in `FTEMPLATES/`. `image_preprocessor.py` renders PDF pages and aligns them against page templates. Alignment and OCR depend on persistent coordinate calibration in `coordinate_maps.json`, derived from field/anchor definitions in `calibration_map.py`.

#### Page Classification and Image Alignment
`aligner_tool.py` orchestrates per-document/page processing. `image_preprocessor.py` uses OpenCV feature matching/homography to identify a page type and geometrically align it to a canonical template. This creates a critical boundary: downstream OCR assumes aligned images and template-specific coordinate maps; template changes require recalibration.

#### OCR Extraction and Normalization
`data_extractor.py` locates text anchors and dynamically derives field ROIs from `coordinate_maps.json`, then invokes system Tesseract via Python OCR libraries. It also supports the calibration workflow. Raw field values flow to `data_postprocessor.py`, which cleans OCR artifacts and formats values such as dates before records are accepted for aggregation.

#### Case-Level Aggregation and Excel Output
Extracted fields from multiple page types are combined around the criminal-case identifier into a unified case row. `excel_exporter.py` writes the consolidated workbook. The working/output archive `Отработано/` contains processed documents and generated spreadsheets, while `dist/EXCEL_REPORTS/` shows packaged-runtime report output.

#### DBF Transformation and Writing
Excel-to-DBF is a separate stage rather than a direct OCR output. Field semantics and target layout are defined by `DBFkeys.py` and `dbf_structure.py`; transformation, validation, and file operations are split across `dbf_transformer.py`, `dbf_creator.py`, and `dbf_writer.py`. Target DBF sets are maintained in `DBF_FILES/`, with baseline/template copies in `DBFTemlates/`.

#### Runtime and External Dependencies
The system runs on Python and requires PyMuPDF for PDF rasterization, OpenCV/Numpy for alignment, Tesseract with Russian language data for OCR, OpenPyXL for XLSX, and a DBF library. It is primarily filesystem-driven—no server or database service is evident. Packaging is PyInstaller-oriented, via `Gemini_OCR_Processor.spec`, `STATCARD_OCR_Processor.spec`, `build_app.py`, and distributable artifacts in `dist/`.

Main entry points: [`image_preprocessor.py`](https://github.com/iivanyaa/staticapp/blob/main/image_preprocessor.py) (Page alignment), [`dbf_transformer.py`](https://github.com/iivanyaa/staticapp/blob/main/dbf_transformer.py) (DBF transformation), [`excel_exporter.py`](https://github.com/iivanyaa/staticapp/blob/main/excel_exporter.py) (Excel exporter), [`aligner_tool.py`](https://github.com/iivanyaa/staticapp/blob/main/aligner_tool.py) (PDF processing CLI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_input["Inputs &amp; Templates"]
  node_scard["Scanned PDFs<br/>input folder"]
  node_references["Card references<br/>reference PDFs"]
  node_templates["Page templates<br/>template assets"]
  node_calibration["Calibration maps<br/>coordinate data"]
  node_calibration_defs["Calibration definitions<br/>field definitions<br/>[calibration_map.py]"]
end

subgraph group_ocr["PDF to Excel"]
  node_aligner{{"PDF processing CLI<br/>orchestrator<br/>[aligner_tool.py]"}}
  node_preprocessor["Page alignment<br/>image processor"]
  node_extractor["OCR extraction<br/>field extractor<br/>[data_extractor.py]"]
  node_postprocessor["Value normalization<br/>data cleaner"]
  node_case_aggregation["Case aggregation<br/>record assembly"]
  node_excel_exporter["Excel exporter<br/>XLSX writer<br/>[excel_exporter.py]"]
  node_processed_archive["Processed archive<br/>output folder"]
end

subgraph group_dbf["Excel to DBF"]
  node_dbf_schema["DBF schema<br/>target layout<br/>[DBFkeys.py]"]
  node_dbf_structure["DBF structure<br/>target layout<br/>[dbf_structure.py]"]
  node_dbf_transformer["DBF transformation<br/>Excel mapper<br/>[dbf_transformer.py]"]
  node_dbf_writer["DBF creation &amp; writing<br/>DBF writer<br/>[dbf_writer.py]"]
  node_dbf_files[("DBF target sets<br/>output folder")]
end

subgraph group_delivery["Runtime &amp; Delivery"]
  node_desktop_runtime["Desktop packaging<br/>Windows distribution"]
end

node_scard -->|"PDF input"| node_aligner
node_aligner -->|"per page"| node_preprocessor
node_references -->|"page references"| node_preprocessor
node_templates -->|"canonical templates"| node_preprocessor
node_preprocessor -->|"aligned page + type"| node_extractor
node_calibration_defs -->|"defines anchors"| node_calibration
node_calibration -->|"field ROIs"| node_extractor
node_extractor -->|"raw OCR fields"| node_postprocessor
node_postprocessor -->|"normalized fields"| node_case_aggregation
node_case_aggregation -->|"case rows"| node_excel_exporter
node_excel_exporter -->|"workbooks"| node_processed_archive
node_excel_exporter -->|"Excel records"| node_dbf_transformer
node_dbf_schema -->|"field mapping"| node_dbf_transformer
node_dbf_structure -->|"record layout"| node_dbf_transformer
node_dbf_transformer -->|"validated DBF records"| node_dbf_writer
node_dbf_writer -->|"DBF files"| node_dbf_files
node_desktop_runtime -.->|"packages pipeline"| node_aligner

click node_scard "https://github.com/iivanyaa/staticapp/tree/main/SCARD"
click node_references "https://github.com/iivanyaa/staticapp/tree/main/EtalonPDFCards"
click node_templates "https://github.com/iivanyaa/staticapp/tree/main/FTEMPLATES"
click node_calibration "https://github.com/iivanyaa/staticapp/blob/main/coordinate_maps.json"
click node_calibration_defs "https://github.com/iivanyaa/staticapp/blob/main/calibration_map.py"
click node_aligner "https://github.com/iivanyaa/staticapp/blob/main/aligner_tool.py"
click node_preprocessor "https://github.com/iivanyaa/staticapp/blob/main/image_preprocessor.py"
click node_extractor "https://github.com/iivanyaa/staticapp/blob/main/data_extractor.py"
click node_postprocessor "https://github.com/iivanyaa/staticapp/blob/main/data_postprocessor.py"
click node_excel_exporter "https://github.com/iivanyaa/staticapp/blob/main/excel_exporter.py"
click node_processed_archive "https://github.com/iivanyaa/staticapp/tree/main/%D0%9E%D1%82%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D0%B0%D0%BD%D0%BE"
click node_dbf_schema "https://github.com/iivanyaa/staticapp/blob/main/DBFkeys.py"
click node_dbf_structure "https://github.com/iivanyaa/staticapp/blob/main/dbf_structure.py"
click node_dbf_transformer "https://github.com/iivanyaa/staticapp/blob/main/dbf_transformer.py"
click node_dbf_writer "https://github.com/iivanyaa/staticapp/blob/main/dbf_writer.py"
click node_dbf_files "https://github.com/iivanyaa/staticapp/tree/main/DBF_FILES"
click node_desktop_runtime "https://github.com/iivanyaa/staticapp/blob/main/dist/STATCARD_PROCESSOR.exe"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_scard,node_references,node_templates,node_calibration,node_calibration_defs toneBlue
class node_aligner,node_preprocessor,node_extractor,node_postprocessor,node_case_aggregation,node_excel_exporter,node_processed_archive toneAmber
class node_dbf_schema,node_dbf_structure,node_dbf_transformer,node_dbf_writer,node_dbf_files toneMint
class node_desktop_runtime toneRose
```

## Components

### Inputs & Templates

- [Scanned PDFs](https://github.com/iivanyaa/staticapp/tree/main/SCARD): input folder · `SCARD`
- [Card references](https://github.com/iivanyaa/staticapp/tree/main/EtalonPDFCards): reference PDFs · `EtalonPDFCards`
- [Page templates](https://github.com/iivanyaa/staticapp/tree/main/FTEMPLATES): template assets · `FTEMPLATES`
- [Calibration maps](https://github.com/iivanyaa/staticapp/blob/main/coordinate_maps.json): coordinate data · Persistent template-specific field and anchor coordinates. · `coordinate_maps.json`
- [Calibration definitions](https://github.com/iivanyaa/staticapp/blob/main/calibration_map.py): field definitions · `calibration_map.py`

### PDF to Excel

- [PDF processing CLI](https://github.com/iivanyaa/staticapp/blob/main/aligner_tool.py): orchestrator · Processes each document and page through alignment and extraction. · `aligner_tool.py`
- [Page alignment](https://github.com/iivanyaa/staticapp/blob/main/image_preprocessor.py): image processor · Classifies pages and aligns them with OpenCV feature matching and homography. · `image_preprocessor.py`
- [OCR extraction](https://github.com/iivanyaa/staticapp/blob/main/data_extractor.py): field extractor · Derives ROIs from aligned pages and coordinate maps, then invokes Tesseract. · `data_extractor.py`
- [Value normalization](https://github.com/iivanyaa/staticapp/blob/main/data_postprocessor.py): data cleaner · `data_postprocessor.py`
- **Case aggregation**: record assembly · Combines page fields into rows keyed by criminal-case identifier.
- [Excel exporter](https://github.com/iivanyaa/staticapp/blob/main/excel_exporter.py): XLSX writer · `excel_exporter.py`
- [Processed archive](https://github.com/iivanyaa/staticapp/tree/main/%D0%9E%D1%82%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D0%B0%D0%BD%D0%BE): output folder · Stores processed documents and generated spreadsheets. · `Отработано`

### Excel to DBF

- [DBF schema](https://github.com/iivanyaa/staticapp/blob/main/DBFkeys.py): target layout · Defines DBF field semantics and structure. · `DBFkeys.py`
- [DBF structure](https://github.com/iivanyaa/staticapp/blob/main/dbf_structure.py): target layout · `dbf_structure.py`
- [DBF transformation](https://github.com/iivanyaa/staticapp/blob/main/dbf_transformer.py): Excel mapper · Transforms and validates Excel records for DBF targets. · `dbf_transformer.py`
- [DBF creation & writing](https://github.com/iivanyaa/staticapp/blob/main/dbf_writer.py): DBF writer · `dbf_writer.py`
- [DBF target sets](https://github.com/iivanyaa/staticapp/tree/main/DBF_FILES): output folder · `DBF_FILES`

### Runtime & Delivery

- [Desktop packaging](https://github.com/iivanyaa/staticapp/blob/main/dist/STATCARD_PROCESSOR.exe): Windows distribution · PyInstaller-oriented desktop runtime and executable output. · `dist/STATCARD_PROCESSOR.exe`

## Connections

- Scanned PDFs → PDF processing CLI: PDF input
- PDF processing CLI → Page alignment: per page
- Card references → Page alignment: page references
- Page templates → Page alignment: canonical templates
- Page alignment → OCR extraction: aligned page + type
- Calibration definitions → Calibration maps: defines anchors
- Calibration maps → OCR extraction: field ROIs
- OCR extraction → Value normalization: raw OCR fields
- Value normalization → Case aggregation: normalized fields
- Case aggregation → Excel exporter: case rows
- Excel exporter → Processed archive: workbooks
- Excel exporter → DBF transformation: Excel records
- DBF schema → DBF transformation: field mapping
- DBF structure → DBF transformation: record layout
- DBF transformation → DBF creation & writing: validated DBF records
- DBF creation & writing → DBF target sets: DBF files
- Desktop packaging → PDF processing CLI: packages pipeline
