-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfigaro.py
More file actions
38 lines (35 loc) · 1.23 KB
/
Copy pathfigaro.py
File metadata and controls
38 lines (35 loc) · 1.23 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
import sys
import os
def remove_watermark(keyword, source, destination=None):
f_name = source.split(".")[0]
t1 = f_name + '_t1.pdf'
t2 = f_name + '_t2.pdf'
if not destination:
destination = f_name + "_clean.pdf"
os.system('qpdf --stream-data=uncompress ' + source + ' ' + t1)
book = open(t1, "rb")
book_clean = open(t2, "wb")
stream, inside_stream, keep = '', False, True
for line in book:
if line == 'stream\n':
inside_stream = True
if inside_stream: stream += line
else: book_clean.write(line)
if keyword in line: keep = False
if 'endstream' in line:
if keep:
book_clean.write(stream)
else:
cnt = len(stream) - len('stream\n\nendstream\n')
book_clean.write('stream\n' + ' ' * cnt + '\nendstream\n')
stream, inside_stream, keep = '', False, True
book_clean.close()
os.system('qpdf --stream-data=compress ' + t2 + ' ' + destination)
os.system('rm ' + t1)
os.system('rm ' + t2)
print 'Success! Wrote to ' + destination
if __name__ == '__main__':
"""
Usage ./figaro.py keyword source.pdf [destination.pdf]
"""
remove_watermark(*sys.argv[1:])