Skip to content

Commit ca9000d

Browse files
wolfram77Lucas-C
andcommitted
Update pdfly/extract_annotated_pages.py
Seems that the `| None` syntax is not valid with Python 3.8 Co-authored-by: Lucas Cimon <[email protected]>
1 parent 1e0ecdd commit ca9000d

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

pdfly/extract_annotated_pages.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,32 @@
66
"""
77

88
from pathlib import Path
9+
from typing import TYPE_CHECKING, Optional
10+
911
from pypdf import PdfReader, PdfWriter
12+
if TYPE_CHECKING:
13+
from pypdf.annotations import AnnotationDictionary
14+
from pypdf.generic import ArrayObject
1015

1116

1217
# Check if an annotation is manipulable.
13-
def is_manipulable(annot) -> bool:
18+
def is_manipulable(annot: AnnotationDictionary) -> bool:
1419
return annot.get("/Subtype") not in ["/Link"]
1520

1621

1722
# Main function.
18-
def main(input_pdf: Path, output_pdf: Path) -> None:
23+
def main(input_pdf: Path, output_pdf: Optional[Path]) -> None:
1924
if not output_pdf:
20-
output_pdf = input_pdf.with_stem(input_pdf.stem + "_annotated")
25+
output_pdf = input_pdf.with_name(input_pdf.stem + "_annotated.pdf")
2126
input = PdfReader(input_pdf)
2227
output = PdfWriter()
2328
output_pages = 0
2429
# Copy only the pages with annotations
2530
for page in input.pages:
26-
if not "/Annots" in page:
31+
if "/Annots" not in page:
2732
continue
28-
if not any(is_manipulable(annot) for annot in page["/Annots"]):
33+
page_annots: ArrayObject = page["/Annots"] # type: ignore[assignment]
34+
if not any(is_manipulable(annot) for annot in page_annots):
2935
continue
3036
output.add_page(page)
3137
output_pages += 1

0 commit comments

Comments
 (0)