From 1c6bc1bc6768499b7b75d4c93713a3b84b9e4c3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Wed, 3 Aug 2022 21:01:03 +0200 Subject: [PATCH] Convert the clipboard content --- poetry.lock | 13 ++++++++++++- pyproject.toml | 1 + scan_to_paperless/scan.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/poetry.lock b/poetry.lock index b23e88b6..2c918ae2 100644 --- a/poetry.lock +++ b/poetry.lock @@ -822,6 +822,14 @@ typing-extensions = {version = "*", markers = "python_version < \"3.10\""} [package.extras] crypto = ["pycryptodome"] +[[package]] +name = "pyperclip" +version = "1.8.2" +description = "A cross-platform clipboard module for Python. (Only handles plain text for now.)" +category = "main" +optional = false +python-versions = "*" + [[package]] name = "pyphen" version = "0.12.0" @@ -1222,7 +1230,7 @@ numpy = "*" [metadata] lock-version = "1.1" python-versions = ">=3.8,<3.11" -content-hash = "82be745efa12a9f6e8b8a8acd869d526515b7fd2a6a10c6ee894ce8725ad7ae1" +content-hash = "820dace8cce046eb3b24dff8400777af2508656844affc1c737df65a2ff88092" [metadata.files] argcomplete = [ @@ -1865,6 +1873,9 @@ pypdf2 = [ {file = "PyPDF2-2.6.0-py3-none-any.whl", hash = "sha256:2b1a869f239e6d538bc8ce49d92ddde74f5c9323e795adb48a2cb5dfa4832709"}, {file = "PyPDF2-2.6.0.tar.gz", hash = "sha256:f52b8af09290cce07750b34e8a7bde501526b59dd1cb4c0a1f21e96ee402c750"}, ] +pyperclip = [ + {file = "pyperclip-1.8.2.tar.gz", hash = "sha256:105254a8b04934f0bc84e9c24eb360a591aaf6535c9def5f29d92af107a9bf57"}, +] pyphen = [ {file = "pyphen-0.12.0-py3-none-any.whl", hash = "sha256:459020cd320eb200c0c5ba46b98b2278fd34c5546f520fdcd2ce5f8d733eb994"}, {file = "pyphen-0.12.0.tar.gz", hash = "sha256:b7d3dfc24b6f2178cdb2b1757ace0bd5d222de3e62c28d22ac578c5f22a13e9b"}, diff --git a/pyproject.toml b/pyproject.toml index c183f119..88d2acaa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -58,6 +58,7 @@ weasyprint = "56.0" cffi = "1.15.1" pikepdf = "5.4.0" zxing-cpp = "1.4.0" +pyperclip = "1.8.2" [tool.poetry.dev-dependencies] prospector = { version = "1.7.7", extras = ["with_bandit", "with_mypy", "with_pyroma"] } diff --git a/scan_to_paperless/scan.py b/scan_to_paperless/scan.py index c8706ee6..93aa8a89 100755 --- a/scan_to_paperless/scan.py +++ b/scan_to_paperless/scan.py @@ -11,6 +11,7 @@ import argcomplete import numpy as np +import pyperclip from ruamel.yaml.main import YAML from skimage import io @@ -44,6 +45,15 @@ def output(cmd: List[str], cmd2: Optional[List[str]] = None, **kwargs: Any) -> b sys.exit(1) +def convert_clipboard() -> None: + """Convert clipboard code from the PDF.""" + original = pyperclip.paste() + new = "\n".join(["" if e == "|" else e for e in original.split("\n")]) + if new != original: + pyperclip.copy(new) + print(new) + + def main() -> None: """Scan a new document.""" parser = argparse.ArgumentParser() @@ -76,10 +86,17 @@ def main() -> None: parser.add_argument( "--set-config", nargs=2, + metavar=("KEY", "VALUE"), action="append", default=[], help="Set a configuration option", ) + parser.add_argument( + "--convert-clipboard", + action="store_true", + help="Wait and convert clipboard content, used to fix the newlines in the copied codes, " + "see requirement: https://pypi.org/project/pyperclip/", + ) argcomplete.autocomplete(parser) args = parser.parse_args() @@ -94,6 +111,17 @@ def main() -> None: yaml.dump(config, sys.stdout) sys.exit() + if args.convert_clipboard: + print("Wait for clipboard content to be converted, press Ctrl+C to stop") + convert_clipboard() + try: + while True: + pyperclip.waitForNewPaste() + convert_clipboard() + except KeyboardInterrupt: + print() + sys.exit() + dirty = False for conf in args.set_config: config[conf[0]] = conf[1] # type: ignore