How deanmalmgren/textract works
1. Project type and scope
- This is a Python text-extraction library, with a small CLI wrapper on top.
- Its core responsibility is: given an input file of many possible formats, return normalized text.
2. Primary entry points
- textract/cli.py is the user-facing command-line interface.
- textract/bin/textract.py is the executable entrypoint/wrapper that invokes the CLI.
- The library package root textract/__init__.py likely exposes the public API for programmatic use.
3. Dispatch and format selection
- The central architectural pattern is format-based dispatch.
- textract/parsers/ contains one parser module per file type, so the system routes input to a specialized handler based on extension or content type.
- The parser set covers text, tabular, office documents, email, HTML, PDF/PS, images, audio, and spreadsheet formats.
4. Parser layer as the main subsystem
- Parsers are the core of the repo: txt, csv, tsv, psv, json, html, eml, msg, epub, doc, docx, odt, rtf, pptx, xls, xlsx, pdf, ps, tiff, png, jpg, gif, wav, mp3, ogg.
- Each parser is responsible for converting its source format into plain text and for handling format-specific edge cases.
- textract/parsers/utils.py likely holds shared parser helpers and normalization logic.
5. Specialized media handling
- The repo separates common media families into grouped modules such as audio.py and image.py, with thin format-specific modules like mp3_parser.py, wav_parser.py, ogg_parser.py, png_parser.py, jpg_parser.py, gif_parser.py, and tiff_parser.py.
- This suggests a shared strategy for OCR or speech-to-text workflows, with per-extension adapters.
6. External engine dependencies
- Some extraction paths depend on external backends rather than pure Python parsing.
- The test fixtures indicate alternative engines for the same format, such as PDF extraction via pdfminer, pdftotext, or OCR via tesseract, and audio transcription via google or sphinx.
- Architecturally, these are optional backend boundaries behind the parser interface.
7. Error and presentation boundaries
- textract/exceptions.py defines repo-specific failure modes, which likely wrap unsupported format, missing dependency, or parse failure conditions.
- textract/colors.py suggests terminal-friendly CLI output styling, separate from the extraction logic itself.
8. Data flow
- Input file path enters through CLI or Python API.
- The dispatcher selects a parser.
- The parser may call a native library or an external command/service.
- The result is normalized into plain text and returned to the caller or printed by the CLI.
9. Testing architecture
- The tests/ tree is organized by file type, with paired raw_text.* and standardized_text.* fixtures.
- This shows the main contract: extracted output should match expected normalized text across many formats.
- There are also tests for special filenames and failure cases, indicating robustness around path handling and unsupported inputs.
10. Documentation and release/tooling
- docs/ is a full Sphinx documentation site, including installation, CLI usage, Python API, and changelog.
- GitHub Actions and uv.lock/pyproject.toml indicate CI and locked dependency management.
- scripts/bump_version.py and the release notes in README.rst show a straightforward packaging/release workflow for a library distributed on PyPI.
11. How to draw the architecture
- Draw a thin CLI/API front end feeding a central parser dispatcher.
- From there, branch into many format-specific parser modules.
- Show several parsers calling out to external engines or libraries for OCR, PDF, audio, or document conversion.
- Keep tests and docs as supporting subsystems around the core extraction pipeline.
Main entry points: __init__.py Dispatcher · cli.py CLI · index.rst Docs · base.py Test suite
Components 17
Entrypoints
User-facing interfaces that invoke text extraction.
- Public API · Library root for programmatic use.
textract/__init__.py - CLI · Command-line entry point for users.
textract/cli.py - Wrapper · Executable launcher that invokes the CLI.
textract/bin/textract.py
Core extraction
Dispatcher, parser families, and backend boundaries.
- Dispatcher · Selects a parser based on input format.
textract/parsers/__init__.py - Parser utils · Common normalization and parser support code.
textract/parsers/utils.py - Text tables · Direct text and tabular text inputs.
textract/parsers/txt_parser.py - Structured docs · Office, email, HTML, EPUB, JSON, and markup formats.
textract/parsers/docx_parser.py - PDF/PS · Extraction for PDF and PostScript documents.
textract/parsers/pdf_parser.py - Spreadsheets · Spreadsheet-oriented extraction paths.
textract/parsers/xls_parser.py - Audio · Speech-to-text extraction family.
textract/parsers/audio.py - Images · OCR-oriented image extraction family.
textract/parsers/image.py - Backends · Optional native libraries, OCR tools, and transcription services.
Support
Errors, presentation, tests, docs, and release tooling.
- Exceptions · Repo-specific parse and dependency errors.
textract/exceptions.py - Colors · Terminal presentation helpers for the CLI.
textract/colors.py - Test suite · Format-by-format contract tests for normalized text output.
tests/base.py - Docs · Installation, CLI, API, and changelog documentation.
docs/index.rst - Release tools · Version bumping, CI, and project metadata.
pyproject.toml
Connections 24
- Wrapper → CLI: invokes · Executable wrapper starts the CLI.
- CLI → Dispatcher: dispatches · CLI routes user input into the extraction pipeline.
- Public API → Dispatcher: calls · Public API exposes programmatic extraction through the dispatcher.
- Dispatcher → Parser utils: uses · Dispatcher and parsers share common helpers.
- Dispatcher → Text tables: routes to · Plain-text and tabular inputs go to text-family parsers.
- Dispatcher → Structured docs: routes to · Document, markup, and email inputs go to structured parsers.
- Dispatcher → PDF/PS: routes to · Page-layout formats go to PDF/PostScript parsers.
- Dispatcher → Spreadsheets: routes to · Spreadsheet formats go to spreadsheet parsers.
- Dispatcher → Audio: routes to · Audio inputs go to speech-to-text parsers.
- Dispatcher → Images: routes to · Image inputs go to OCR parsers.
- Text tables → Backends: direct · Mostly direct parsing with minimal external dependencies.
- Structured docs → Backends: depends on · May rely on document libraries or format-specific engines.
- PDF/PS → Backends: depends on · Can use pdfminer, pdftotext, or OCR paths.
- Spreadsheets → Backends: depends on · Uses spreadsheet readers and conversion backends.
- Audio → Backends: depends on · Can call external transcription services or engines.
- Images → Backends: depends on · Uses OCR backends for image text extraction.
- CLI → Colors: formats output · CLI presentation uses terminal styling helpers.
- Dispatcher → Exceptions: raises · Unsupported formats and parse failures surface as repo errors.
- CLI → Exceptions: handles · CLI turns extraction errors into user-facing failure messages.
- Test suite → Dispatcher: verifies · Fixture suite exercises the extraction pipeline across formats.
- Test suite → Backends: covers · Tests encode alternative backend outputs for the same format.
- Docs → CLI: documents · Docs describe CLI usage and options.
- Docs → Public API: documents · Docs describe the Python API.
- Release tools → Docs: supports · Project tooling supports docs and distribution workflow.