Commit ca9000d 1 parent 1e0ecdd commit ca9000d Copy full SHA for ca9000d
File tree 1 file changed +11
-5
lines changed
1 file changed +11
-5
lines changed Original file line number Diff line number Diff line change 6
6
"""
7
7
8
8
from pathlib import Path
9
+ from typing import TYPE_CHECKING , Optional
10
+
9
11
from pypdf import PdfReader , PdfWriter
12
+ if TYPE_CHECKING :
13
+ from pypdf .annotations import AnnotationDictionary
14
+ from pypdf .generic import ArrayObject
10
15
11
16
12
17
# Check if an annotation is manipulable.
13
- def is_manipulable (annot ) -> bool :
18
+ def is_manipulable (annot : AnnotationDictionary ) -> bool :
14
19
return annot .get ("/Subtype" ) not in ["/Link" ]
15
20
16
21
17
22
# Main function.
18
- def main (input_pdf : Path , output_pdf : Path ) -> None :
23
+ def main (input_pdf : Path , output_pdf : Optional [ Path ] ) -> None :
19
24
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 " )
21
26
input = PdfReader (input_pdf )
22
27
output = PdfWriter ()
23
28
output_pages = 0
24
29
# Copy only the pages with annotations
25
30
for page in input .pages :
26
- if not "/Annots" in page :
31
+ if "/Annots" not in page :
27
32
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 ):
29
35
continue
30
36
output .add_page (page )
31
37
output_pages += 1
You can’t perform that action at this time.
0 commit comments