Skip to content

Commit

Permalink
allow to sort files by DATE or NAME when merging all files in dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobi208 committed May 8, 2024
1 parent afecafe commit dc73bea
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 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
29 changes: 26 additions & 3 deletions src/pypdf_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from os import makedirs, listdir, getcwd
from os import makedirs, scandir, getcwd
from os.path import basename, dirname

import click
Expand Down Expand Up @@ -127,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 @@ -252,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 @@ -264,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 dc73bea

Please sign in to comment.