diff --git a/.pyrefly-baseline.json b/.pyrefly-baseline.json index 3c09a1099..d8b7c7eb7 100644 --- a/.pyrefly-baseline.json +++ b/.pyrefly-baseline.json @@ -896,13 +896,6 @@ "concise_description": "Unexpected keyword argument `fast` in function `tantivy.tantivy.SchemaBuilder.add_json_field`", "severity": "error" }, - { - "column": 9, - "path": "src/documents/serialisers.py", - "name": "bad-override-param-name", - "concise_description": "Class member `MatchingModelSerializer.validate` overrides parent class `ModelSerializer` in an inconsistent manner", - "severity": "error" - }, { "column": 62, "path": "src/documents/serialisers.py", @@ -952,13 +945,6 @@ "concise_description": "Cannot set item in `Mapping[str, Any]`", "severity": "error" }, - { - "column": 9, - "path": "src/documents/serialisers.py", - "name": "bad-override-param-name", - "concise_description": "Class member `TagSerializer.validate` overrides parent class `MatchingModelSerializer` in an inconsistent manner", - "severity": "error" - }, { "column": 17, "path": "src/documents/serialisers.py", diff --git a/docs/usage.md b/docs/usage.md index aa054e741..48417c3c0 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -109,7 +109,7 @@ Think of versions as **file history** for a document. Paperless-ngx includes management lists for tags, correspondents, document types and more. These areas allow you to view, add, edit, delete and manage permissions for these objects. You can also manage saved views, mail accounts, mail rules, -workflows and more from the management sections. +workflows, [OCR templates](#ocr-templates) and more from the management sections. ### Nested Tags @@ -821,6 +821,99 @@ The following custom field types are supported: - `Document Link`: reference(s) to other document(s) displayed as links, automatically creates a symmetrical link in reverse - `Select`: a pre-defined list of strings from which the user can choose +## OCR Templates {#ocr-templates} + +OCR templates read text from fixed areas ("zones") of a document and write it to a +[custom field](#custom-fields), or to the document's title, archive serial number or created date. They are +useful for documents that always share the same layout, such as bills from a particular utility. They allow you +to draw a zone around the account number once, and every new bill gets its account number filled in. + +Like tags or document types, each template has a [matching algorithm](advanced_usage.md#matching) that +decides which documents it automatically applies to (or not), based on their content. + +### Creating a template + +1. Open **OCR Templates** under "Manage" and choose **Add Template**. +2. Give the template a name and set up its matching, for example **Any word** with the utility's name. Choose + **None** for a template that should only ever be run manually. +3. Search for a sample document with the layout the template is for. Its pages are shown next to the template + settings. +4. Draw zones on the document. Click a zone to select and edit it. +5. Choose the field each zone fills. +6. Use **Test** to run OCR on the selected zone of the sample document and see the text it finds and the value + that would be written. + +Zones are positioned relative to the sample document's page, so templates work best when the documents they +match have an identical layout. Keep the matching specific enough that it only catches documents with that +layout. When more than one template applies to a document, the first one by name fills a field and later +templates do not overwrite it. + +### Zone settings + +- **Page**: the page the zone is on. Turn on **Last page** for zones that are always on the final page, + regardless of how many pages a document has. If the number of pages cannot be determined, the zone is skipped. +- **OCR language**: the Tesseract languages used to read the zone. Languages other than the defaults must be + installed, see [`PAPERLESS_OCR_LANGUAGES`](configuration.md#PAPERLESS_OCR_LANGUAGES). +- **Transform**: post-processing applied to the text, such as stripping whitespace, keeping only numbers, + parsing a date or reading a QR code or barcode instead of text. +- **Date format**: for the "Parse date" transform, the format of the date using + [Python date codes](https://docs.python.org/3/library/datetime.html#format-codes), e.g. `%d.%m.%Y`. + Leave it empty to detect the format automatically. +- **Validation regex**: a regular expression the whole value must match. Values that do not match are not + written. + +Zones can fill custom fields of type text, long text, URL, date, integer, number, monetary or boolean. +Document link and select fields are not supported. + +### Combining zones + +When several zones fill the same field, a **Combined value** setting appears for that field. Use it to arrange +the zones' values with `{Zone name}` tokens and any text in between, for example `{Start} - {End}`. If it +is left empty, the values are joined with spaces in zone order. + +### When templates run + +When a document is added, every enabled template that matches its content runs automatically, reading the +zones from the archived file, or from the original PDF or image when there is no archived file. A template is +only used if the document's owner can view it, so users' templates apply to their own documents, and unowned +templates apply to everyone's. The owner here is the one set by the upload, the mail rule or a consumption +workflow. A document without an owner at that point, for example from the consume folder or a mail rule +without an owner, considers every enabled template. An owner assigned later by a "Document Added" workflow +does not change which templates ran. Templates run in name order, so if two templates fill the same field, the +later one wins. + +Zone OCR keeps values supplied when the document is consumed: an uploaded title, ASN, created date or custom +field value, a mail rule's title, a barcode ASN, and values assigned by a consumption workflow. Zones fill the +remaining fields before "Document Added" workflows run, so those workflows can use or overwrite the values. + +Turn off **Enabled** to pause a template without deleting it. Disabled templates never run automatically, but +can still be run manually. + +### Running templates manually + +To run templates on documents that are already in Paperless-ngx, open the **Actions** menu on the document +details page: + +- **Run Zone OCR** runs every enabled template that matches the document. +- The button next to it opens a dialog where you can choose one template to run + instead. A template you choose runs even if it does not match the document or is disabled. + +Both actions use the file version selected in document details. The latest version is selected by default. +Extracted values update the document's metadata, which is shared across its versions. If no template +applies to the document, nothing is run. Otherwise the run is processed in the background and appears in the +task list. The document shows the extracted values once the task finishes. + +The bulk editor's **Run Zone OCR** action opens the same dialog for the selected documents. Bulk runs use +the latest version of each document and are processed in the background. + +!!! warning + + Running zone OCR manually or in bulk overwrites the fields it fills. A zone that finds no value leaves its + field unchanged. + +Managing OCR templates requires the OCR template permissions. Running zone OCR on documents requires +permission to change them, and choosing a template requires permission to view it. + ## PDF Actions Paperless-ngx supports basic editing operations for PDFs (these operations currently cannot be performed on non-PDF files). When viewing an individual document you can diff --git a/src-ui/src/app/app-routing.module.ts b/src-ui/src/app/app-routing.module.ts index 12a446387..3a8dd9845 100644 --- a/src-ui/src/app/app-routing.module.ts +++ b/src-ui/src/app/app-routing.module.ts @@ -13,6 +13,7 @@ import { DocumentDetailComponent } from './components/document-detail/document-d import { DocumentListComponent } from './components/document-list/document-list.component' import { DocumentAttributesComponent } from './components/manage/document-attributes/document-attributes.component' import { MailComponent } from './components/manage/mail/mail.component' +import { OcrTemplatesComponent } from './components/manage/ocr-templates/ocr-templates.component' import { SavedViewsComponent } from './components/manage/saved-views/saved-views.component' import { ShareLinksComponent } from './components/manage/share-links/share-links.component' import { WorkflowsComponent } from './components/manage/workflows/workflows.component' @@ -275,6 +276,18 @@ export const routes: Routes = [ componentName: 'WorkflowsComponent', }, }, + { + path: 'ocr-templates', + component: OcrTemplatesComponent, + canActivate: [PermissionsGuard], + data: { + requiredPermission: { + action: PermissionAction.View, + type: PermissionType.OcrTemplate, + }, + componentName: 'OcrTemplatesComponent', + }, + }, { path: 'mail', component: MailComponent, diff --git a/src-ui/src/app/components/admin/settings/settings.component.ts b/src-ui/src/app/components/admin/settings/settings.component.ts index 1fd6d1ac9..c1a51e53d 100644 --- a/src-ui/src/app/components/admin/settings/settings.component.ts +++ b/src-ui/src/app/components/admin/settings/settings.component.ts @@ -113,6 +113,7 @@ const sidebarItemLabels: Record = { [HideableSidebarItemID.SavedViews]: $localize`Saved Views`, [HideableSidebarItemID.ShareLinks]: $localize`Share Links`, [HideableSidebarItemID.Workflows]: $localize`Workflows`, + [HideableSidebarItemID.OcrTemplates]: $localize`OCR Templates`, [HideableSidebarItemID.Mail]: $localize`Mail`, [HideableSidebarItemID.Documentation]: $localize`Documentation`, } diff --git a/src-ui/src/app/components/app-frame/app-frame.component.html b/src-ui/src/app/components/app-frame/app-frame.component.html index a7b4cdeec..e8f07350d 100644 --- a/src-ui/src/app/components/app-frame/app-frame.component.html +++ b/src-ui/src/app/components/app-frame/app-frame.component.html @@ -289,6 +289,17 @@ } +