-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 with extract-annotated-pages command
- Loading branch information
Showing
3 changed files
with
82 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Extract only the annotated pages from a PDF. | ||
Q: Why does this help? | ||
A: https://github.com/py-pdf/pdfly/issues/97 | ||
""" | ||
|
||
from pathlib import Path | ||
from pypdf import PdfReader, PdfWriter | ||
|
||
|
||
# Check if an annotation is manipulable. | ||
def is_manipulable(annot) -> bool: | ||
return annot.get("/Subtype") not in ["/Link"] | ||
|
||
|
||
# Main function. | ||
def main(input_pdf: Path, output_pdf: Path) -> None: | ||
if not output_pdf: | ||
output_pdf = input_pdf.with_stem(input_pdf.stem + "_annotated") | ||
input = PdfReader(input_pdf) | ||
output = PdfWriter() | ||
output_pages = 0 | ||
# Copy only the pages with annotations | ||
for page in input.pages: | ||
if not "/Annots" in page: continue | ||
if not any(is_manipulable(annot) for annot in page["/Annots"]): continue | ||
output.add_page(page) | ||
output_pages += 1 | ||
# Save the output PDF | ||
output.write(output_pdf) | ||
print(f"Extracted {output_pages} pages with annotations to {output_pdf}") |