Skip to content

Commit

Permalink
Merge pull request #9 from Tobi208/8-sorted-merge
Browse files Browse the repository at this point in the history
sorted merge
  • Loading branch information
Tobi208 authored May 8, 2024
2 parents ed8e353 + dc73bea commit 87c0c45
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,12 @@ Usage: pypdf-cli merge [OPTIONS] [INPUT_FILES]...
INPUT_FILES are the locations of at least two pdf files to be merged.
Options:
-o, --output PATH Optional location of the output pdf file. WARNING:
overwrites existing files.
--help Show this message and exit.
-o, --output PATH Optional location of the output pdf file. WARNING:
overwrites existing files.
-a, --all Select every index.
-s, --sort SORT BY Sort input files by NAME or DATE (last modified) when
selecting all files.
--help Show this message and exit.
```

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi"

[project]
name = "pypdf-cli"
version = "1.0.0"
version = "1.0.1"
authors = [ { name = "Tobias Lass", email = "tobi208@github.com" } ]
description = "A Python-based CLI that allows for comfortable every-day PDF manipulation with pypdf."
readme = "README.md"
Expand All @@ -16,7 +16,7 @@ classifiers = [
requires-python = ">=3.6"
dependencies = [
"click>=8.0.3",
"pypdf>=3.0.0",
"pypdf>=3.1.0",
"setuptools>=68.2.2",
]
license = { file = "LICENSE" }
Expand Down
34 changes: 28 additions & 6 deletions src/pypdf_cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from os import makedirs, listdir, getcwd
from os import makedirs, scandir, getcwd
from os.path import basename, dirname

import click
from pypdf import PdfReader, PdfWriter
from pypdf.errors import PdfReadError
from pkg_resources import get_distribution


# auxiliary functions
Expand Down Expand Up @@ -99,8 +98,8 @@ def write(writer, output):

# click functions

@click.group()
@click.version_option(get_distribution('pypdf-cli').version)
@click.group(invoke_without_command=True, no_args_is_help=True)
@click.version_option()
def cli():
pass

Expand Down Expand Up @@ -128,12 +127,26 @@ def convert(self, value, param, ctx):

return selection


class SortByType(click.ParamType):

name = "SORT BY"

def convert(self, value, param, ctx):

if value.upper() not in ['NAME', 'DATE']:
self.fail(f'{value!r} is not a valid metric to sort the input files by. Use NAME or DATE.', param, ctx)
else:
return value.upper()

INT_PAGES = PagesType()
SORT_BY = SortByType()

OUTPUT_HELP = 'Optional location of the output pdf file. WARNING: overwrites existing files.'
PAGES_MULTI_HELP = 'Selection of pages. Enter list of integers and ranges without spaces or wrap in quotation marks. E.g. 1,3-5,7.'
PAGES_SINGLE_HELP = 'Selection of page. Enter integer. E.g. 2.'
ALL_HELP = 'Select every index.'
SORT_BY_HELP = 'Sort input files by NAME or DATE (last modified) when selecting all files.'


def common_options(f):
Expand Down Expand Up @@ -253,7 +266,8 @@ def insert(input_files, output, select_pages):
@click.argument('input-files', nargs=-1, type=click.Path(exists=True), required=False)
@click.option('--output', '-o', type=click.Path(), help=OUTPUT_HELP)
@click.option('--all', '-a', is_flag=True, default=False, help=ALL_HELP)
def merge(input_files, output, all):
@click.option('--sort', '-s', type=SORT_BY, default='NAME', help=SORT_BY_HELP)
def merge(input_files, output, all, sort):
"""
Merge two or more pdf files.
Files are appended in the order they are entered.
Expand All @@ -265,7 +279,15 @@ def merge(input_files, output, all):
# if no input files are specified or 'all' flag is set,
# take all pdf files in current directory
if len(input_files) == 0 or all:
input_files = [f for f in listdir(getcwd()) if f.endswith('.pdf')]
with scandir(getcwd()) as scanner:
input_files = [f for f in scanner if f.name.endswith('.pdf')]

if sort == 'DATE':
input_files.sort(key=lambda f: float(f.stat().st_mtime))
else: # default NAME
input_files.sort(key=lambda f: f.name)

input_files = [f.name for f in input_files]

# specific verification
if len(input_files) < 2:
Expand Down

0 comments on commit 87c0c45

Please sign in to comment.