- Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathbisect_pdf.py
42 lines (37 loc) · 1.37 KB
/
bisect_pdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2024 James R. Barlow
# SPDX-License-Identifier: MIT
"""Helper script for bisecting PDFs to find a page with an issue."""
importsys
importpikepdf
iflen(sys.argv) !=2:
print(f"Usage: {sys.argv[0]} <input.pdf>")
sys.exit(1)
withpikepdf.open(sys.argv[1]) aspdf:
num_pages=len(pdf.pages)
low=0
high=num_pages-1
whilelow<=high:
mid= (low+high) //2
withpikepdf.new() asnew_pdf:
new_pdf.pages.extend(pdf.pages[low : mid+1])
new_pdf.save(f"bisect-issue-{low+1}-{mid+1}.pdf")
print(f"Is bisect-issue-{low+1}-{mid+1}.pdf good or bad?", end=" ")
whileTrue:
response=input().lower()
ifresponse=="good":
low=mid+1
break
elifresponse=="bad":
high=mid-1
break
else:
print("Please respond with 'good' or 'bad'.")
print(f"The issue is on page {low+1} of the original PDF.")
withpikepdf.new() asnew_pdf:
new_pdf.pages.extend(pdf.pages[low])
new_pdf.save(f"bisect-issue-bad-{low+1}.pdf")
withpikepdf.new() asnew_pdf:
new_pdf.pages.extend(pdf.pages[:low])
new_pdf.pages.extend(pdf.pages[low+1 :])
new_pdf.save(f"bisect-issue-good-{low+1}.pdf")