document2md.batch¶
BatchConverter, the package’s public Python
entry point, and the backend selector it resolves and dispatches on.
Batch document-to-Markdown conversion, keeping one mineru-api server warm across many jobs instead of paying its startup (and model-loading) cost once per document:
- with BatchConverter() as convert:
- for pdf_path, outdir, filename in jobs:
convert(pdf_path, outdir, filename)
document2md itself has no notion of what a “note” is or where a document came from — a job is just a PDF (a single path) or a set of scanned page images (a list of paths), an output directory, and an output filename. Whatever calls this decides what those mean (a DOF legal provision, or anything else).
- class BatchConverter[source]¶
Context manager: __enter__ resolves the requested backend, starts a persistent mineru-api server if the resolved backend needs one (skipped if a caller further up already has one running via MINERU_API_URL), and returns self, callable once per document; __exit__ stops it. Calling it converts one document — a single PDF path, or a list of image paths for a document spanning several scanned pages — to Markdown, written to outdir/filename.
backend names who is responsible for the conversion: “auto” (default), “mineru” or “pymupdf”; any other value raises ValueError. “auto” resolves to “mineru” when the mineru CLI is on PATH, otherwise to “pymupdf” — mineru remains the reference backend where it is installed; the light backend only makes installing it optional. The resolved name is stored as self.backend once __enter__ has run.
“pymupdf” reads a PDF’s own embedded text layer instead of running OCR — far faster, at the cost of slightly worse structure — and raises RuntimeError (naming the mineru extra) on input it cannot handle: a list of page images (which always need OCR), or a PDF without enough of a text layer (see document2md.pymupdf_backend.has_text_layer). There is no silent fallback to mineru.
titulo/titulo_siguiente, when given, slice the OCR’d Markdown down to the text between their two boundaries (see document2md.cutter.cut_markdown_by_titles) — e.g. a DOF legal provision’s own title and the next one’s, to cut a page shared with the notes before and after it down to just this one. Left out (the default), the whole conversion is kept as-is, on the assumption that the whole document is what was asked for.