-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathexample.py
More file actions
58 lines (45 loc) · 1.51 KB
/
example.py
File metadata and controls
58 lines (45 loc) · 1.51 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
#;encoding=utf-8
# Example file to redact Social Security Numbers from the
# text layer of a PDF and to demonstrate metadata filtering.
import re
from datetime import datetime
import pdf_redactor
## Set options.
options = pdf_redactor.RedactorOptions()
options.metadata_filters = {
# Perform some field filtering --- turn the Title into uppercase.
"Title": [lambda value : value.upper()],
# Set some values, overriding any value present in the PDF.
"Producer": [lambda value : "My Name"],
"CreationDate": [lambda value : datetime.utcnow()],
# Clear all other fields.
"DEFAULT": [lambda value : None],
}
# Clear any XMP metadata, if present.
options.xmp_filters = [lambda xml : None]
# Redact things that look like social security numbers, replacing the
# text with X's.
options.content_filters = [
# First convert all dash-like characters to dashes.
(
re.compile(u"[−–—~‐]"),
lambda m : "-"
),
# Then do an actual SSL regex.
# See https://github.com/opendata/SSN-Redaction for why this regex is complicated.
(
re.compile(r"(?<!\d)(?!666|000|9\d{2})([OoIli0-9]{3})([\s-]?)(?!00)([OoIli0-9]{2})\2(?!0{4})([OoIli0-9]{4})(?!\d)"),
lambda m : "XXX-XX-XXXX"
),
# Content filter that runs on the text comment annotation body.
(
re.compile(r"comment!"),
lambda m : "annotation?"
),
]
# Filter the link target URI.
options.link_filters = [
lambda href, annotation : "https://www.google.com"
]
# Perform the redaction using PDF on standard input and writing to standard output.
pdf_redactor.redactor(options)