Skip to content

Commit

Permalink
MAINT: In the cat subcommand, replace the usage of the deprecated Pdf…
Browse files Browse the repository at this point in the history
…Merger by PdfWriter (#34)

Replaced the deprecated pypdf.PdfMerger with pypdf.PdfReader

Closes #31
  • Loading branch information
kommade authored Dec 16, 2023
1 parent 2deee1c commit f31111c
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pdfly/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
from pathlib import Path
from typing import List, Tuple

from pypdf import PageRange, PdfMerger, parse_filename_page_ranges
from pypdf import PageRange, PdfReader, PdfWriter, parse_filename_page_ranges


def main(
Expand All @@ -63,16 +63,19 @@ def main(
sys.stdout.flush()
output_fh = os.fdopen(sys.stdout.fileno(), "wb")

merger = PdfMerger()
writer = PdfWriter()
in_fs = {}
try:
for filename, page_range in filename_page_ranges: # type: ignore
if verbose:
print(filename, page_range, file=sys.stderr)
if filename not in in_fs:
in_fs[filename] = open(filename, "rb")
merger.append(in_fs[filename], pages=page_range)
merger.write(output_fh)

reader = PdfReader(in_fs[filename])
for page_num in range(*page_range.indices(len(reader.pages))):
writer.add_page(reader.pages[page_num])
writer.write(output_fh)
except Exception:
print(traceback.format_exc(), file=sys.stderr)
print(f"Error while reading {filename}", file=sys.stderr)
Expand Down

0 comments on commit f31111c

Please sign in to comment.