Skip to content

Commit 9446969

Browse files
committed
Add rm module
1 parent 5ecfb98 commit 9446969

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

pdfly/cli.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import pdfly.extract_images
1616
import pdfly.metadata
1717
import pdfly.pagemeta
18+
import pdfly.rm
1819
import pdfly.up2
1920
import pdfly.x2pdf
2021

@@ -104,7 +105,7 @@ def cat(
104105
pdfly.cat.main(filename, fn_pgrgs, output, verbose)
105106

106107

107-
@entry_point.command(name="rm") # type: ignore[misc]
108+
@entry_point.command(name="rm", help=pdfly.rm.__doc__)
108109
def rm(
109110
filename: Annotated[
110111
Path,
@@ -125,9 +126,7 @@ def rm(
125126
False, help="show page ranges as they are being read"
126127
),
127128
) -> None:
128-
pdfly.cat.main(
129-
filename, fn_pgrgs, output, verbose, inverted_page_selection=True
130-
)
129+
pdfly.rm.main(filename, fn_pgrgs, output, verbose)
131130

132131

133132
@entry_point.command(name="meta", help=pdfly.metadata.__doc__) # type: ignore[misc]

pdfly/rm.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Remove pages from PDF files.
3+
4+
Page ranges refer to the previously-named file.
5+
A file not followed by a page range means all the pages of the file.
6+
7+
PAGE RANGES are like Python slices.
8+
9+
Remember, page indices start with zero.
10+
11+
Page range expression examples:
12+
13+
: all pages. -1 last page.
14+
22 just the 23rd page. :-1 all but the last page.
15+
0:3 the first three pages. -2 second-to-last page.
16+
:3 the first three pages. -2: last two pages.
17+
5: from the sixth page onward. -3:-1 third & second to last.
18+
19+
The third, "stride" or "step" number is also recognized.
20+
21+
::2 0 2 4 ... to the end. 3:0:-1 3 2 1 but not 0.
22+
1:10:2 1 3 5 7 9 2::-1 2 1 0.
23+
::-1 all pages in reverse order.
24+
25+
Examples
26+
pdfly rm -o output.pdf document.pdf 2:5
27+
28+
Remove pages 2 to 4 from document.pdf, producing output.pdf.
29+
30+
pdfly rm document.pdf :-1
31+
32+
Removes all pages except the last one from document.pdf, modifying the original file.
33+
34+
pdfly rm report.pdf :6 7:
35+
36+
Remove all pages except page seven from report.pdf,
37+
producing a single-page report.pdf.
38+
39+
"""
40+
41+
from pathlib import Path
42+
from typing import List
43+
44+
from pdfly.cat import main as cat_main
45+
46+
47+
def main(
48+
filename: Path, fn_pgrgs: List[str], output: Path, verbose: bool
49+
) -> None:
50+
cat_main(filename, fn_pgrgs, output, verbose, inverted_page_selection=True)

0 commit comments

Comments
 (0)