Skip to content

Custom field sum

Following scripts output the sum of all values stored in a custom field for documents matching certain criteria.

Sum of single field

custom-field-sum.py
#!/usr/bin/env python

import os
import asyncio
from pypaperless import Paperless

PAPERLESS_URL = os.environ["PAPERLESS_URL"]
PAPERLESS_TOKEN = os.environ["PAPERLESS_TOKEN"]

# Custom field IDs to sum up
CUSTOM_FIELD_ID = 1

# Visit https://your-paperless-url/api/documents in your browser
# to discover all available filters
filters = {
    "tags__id": 38,
    "document_type__id": 3,
    "created__date__gt": "2023-10-01",
    "created__date__lt": "2023-12-31"
}

async def main():
    paperless = Paperless(PAPERLESS_URL, PAPERLESS_TOKEN)

    async with paperless:
        async with paperless.documents.reduce(**filters) as filtered:
            sum = 0
            async for doc in filtered:
                sum += [f.value for f in doc.custom_fields if f.field == CUSTOM_FIELD_ID][0]

    print(sum)

asyncio.run(main())

Sum of difference of two fields

Appears to be an exotic use-case. But imagine having two custom fields for an invoice: gross and net value. To get to know the sum of all taxes paid, you will need to sum up the differences of both custom fields. As simple as that :)

custom-field-sum-of-differences.py
#!/usr/bin/env python

import os
import asyncio
from pypaperless import Paperless

PAPERLESS_URL = os.environ["PAPERLESS_URL"]
PAPERLESS_TOKEN = os.environ["PAPERLESS_TOKEN"]

# Custom field IDs whose differences should be totaled
CUSTOM_FIELD_ID_MINUEND = 1
CUSTOM_FIELD_ID_SUBTRAHEND = 2

async def main():
    paperless = Paperless(PAPERLESS_URL, PAPERLESS_TOKEN)

    # Visit https://your-paperless-url/api/documents in your browser
    # to discover all available filters
    filters = {
        "tags__id": 38,
        "document_type__id": 3,
        "created__date__gt": "2023-10-01",
        "created__date__lt": "2023-12-31"
    }

    async with paperless:
        async with paperless.documents.reduce(**filters) as filtered:
            sum = 0
            async for doc in filtered:
                minuend = [f.value for f in doc.custom_fields if f.field == CUSTOM_FIELD_ID_MINUEND][0]
                subtrahend = [f.value for f in doc.custom_fields if f.field == CUSTOM_FIELD_ID_SUBTRAHEND][0]
                sum += minuend - subtrahend

            print(sum)

asyncio.run(main())