How do I upload PDFs to Paperless NGX with Python? #1

Closed
opened 2025-12-30 12:43:03 +00:00 by jadudm · 1 comment
Owner

I want to watch a directory on my local machine for PDFs, and then upload them to Paperless NGX whenever they appear. That way, I can scan directly to a folder with NAPS2, and the uploader will do its thing.

I want to watch a directory on my local machine for PDFs, and then upload them to [Paperless NGX](https://github.com/paperless-ngx/paperless-ngx) whenever they appear. That way, I can scan directly to a folder with [NAPS2](https://www.naps2.com/), and the uploader will do its thing.
Author
Owner

I wrote this little script. You need to set

  • PAPERLESS_API_TOKEN
  • PAPERLESS_API_HOST

in the environment. Paperless will let you get an API token from your profile, and the host should be just the hostname (e.g. paperless.example.com).

import requests
import os
from time import sleep
import re
import shutil
from http import HTTPStatus

PAPERLESS = f"https://{os.getenv('PAPERLESS_API_HOST')}/api/documents/post_document/"
TOKEN = os.getenv("PAPERLESS_API_TOKEN")

while True:
    files = os.listdir()
    for file in files:
        m = re.match(r".*\.pdf$", file)
        if m:
            pdf = m.group(0)
            # https://stackoverflow.com/questions/22567306/how-to-upload-file-with-python-requests
            resp = requests.post(
                PAPERLESS,
                headers={"Authorization": f"Token {TOKEN}"},
                files={"document": open(pdf, "rb")},
            )
            print(f"{pdf}: {resp.status_code} {resp.text}")
            if resp.status_code == HTTPStatus.OK:
                shutil.move(pdf, os.path.join("success", pdf))
            else:
                shutil.move(pdf, os.path.join("failure", pdf))

    sleep(5)
I wrote this little script. You need to set * `PAPERLESS_API_TOKEN` * `PAPERLESS_API_HOST` in the environment. Paperless will let you get an API token from your profile, and the host should be just the hostname (e.g. `paperless.example.com`). ```python import requests import os from time import sleep import re import shutil from http import HTTPStatus PAPERLESS = f"https://{os.getenv('PAPERLESS_API_HOST')}/api/documents/post_document/" TOKEN = os.getenv("PAPERLESS_API_TOKEN") while True: files = os.listdir() for file in files: m = re.match(r".*\.pdf$", file) if m: pdf = m.group(0) # https://stackoverflow.com/questions/22567306/how-to-upload-file-with-python-requests resp = requests.post( PAPERLESS, headers={"Authorization": f"Token {TOKEN}"}, files={"document": open(pdf, "rb")}, ) print(f"{pdf}: {resp.status_code} {resp.text}") if resp.status_code == HTTPStatus.OK: shutil.move(pdf, os.path.join("success", pdf)) else: shutil.move(pdf, os.path.join("failure", pdf)) sleep(5) ```
jadudm added the bashtoolspython labels 2025-12-30 12:51:33 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: jadudm/how-to-do-computer-things#1