-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrip_pdf_metadata.py
More file actions
executable file
·78 lines (66 loc) · 1.78 KB
/
strip_pdf_metadata.py
File metadata and controls
executable file
·78 lines (66 loc) · 1.78 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.12"
# category = "data"
# dependencies = [
# "typer>=0.15.0",
# "rich>=13.0.0",
# "pikepdf",
# ]
# ///
"""
Strip metadata from a single PDF file.
"""
import sys
from pathlib import Path
from typing import Annotated
import pikepdf
import typer
from rich import print
def strip_metadata(src: Path, dst: Path) -> None:
"""Strip metadata from a PDF file."""
try:
with pikepdf.open(src) as pdf:
root = pdf.trailer.get("/Root")
if root and "/Metadata" in root:
del root["/Metadata"]
info = pdf.trailer.get("/Info")
if info:
for k in list(info.keys()):
del info[k]
del pdf.trailer["/Info"]
pdf.save(dst)
print(f"[green]Successfully stripped metadata:[/green] {src} -> {dst}")
except Exception as e:
print(f"[bold red]Error:[/bold red] stripping metadata from '{src}': {e}")
sys.exit(1)
def main(
input_file: Annotated[
Path,
typer.Argument(
help="Input PDF file.",
exists=True,
dir_okay=False,
resolve_path=True,
),
],
output_file: Annotated[
Path | None,
typer.Argument(
help="Output PDF file. Defaults to 'stripped_<INPUT_FILE>'.",
dir_okay=False,
resolve_path=True,
),
] = None,
) -> None:
"""
Strip metadata from a PDF file.
If OUTPUT_FILE is not provided, writes to 'stripped_<INPUT_FILE>'.
"""
if output_file:
dst = output_file
else:
dst = input_file.with_name(f"stripped_{input_file.name}")
strip_metadata(input_file, dst)
if __name__ == "__main__":
typer.run(main)