-
Notifications
You must be signed in to change notification settings - Fork 4
/pdf
Copy pathexecutable file
·59 lines (48 loc) · 1.5 KB
/pdf
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
#!/usr/bin/env python
import sys
from os import path
from pypdf import PdfReader, PdfWriter
def print_info(pdf):
pdf = PdfReader(pdf)
info = pdf.metadata
print(f'Author: {info.author}\n'
f'Creator: {info.creator}\n'
f'Producer: {info.producer}\n'
f'Subject: {info.subject}\n'
f'Title: {info.title}\n'
f'Number of pages: {len(pdf.pages)}')
def rotate(angle, pdfin, pdfout):
reader = PdfReader(pdfin)
writer = PdfWriter()
for page in reader.pages:
writer.add_page(page.rotate(angle))
with open(pdfout, 'wb') as f:
writer.write(f)
def split(pdfin, pdfout):
reader = PdfReader(pdfin)
name, ext = path.splitext(pdfout)
for i, page in enumerate(reader.pages):
writer = PdfWriter()
writer.add_page(page)
with open(f'{name}-{i}{ext}', 'wb') as f:
writer.write(f)
def merge(pdfin, pdfout):
writer = PdfWriter()
for pdf in pdfin:
reader = PdfReader(pdf)
for page in reader.pages:
writer.add_page(page)
with open(pdfout, 'wb') as f:
writer.write(f)
if __name__ == '__main__':
action = sys.argv[1]
if action == 'info':
print_info(sys.argv[2])
elif action == 'rotate':
rotate(int(sys.argv[2]), sys.argv[3], sys.argv[4])
elif action == 'split':
split(sys.argv[2], sys.argv[3])
elif action == 'merge':
merge(sys.argv[2:-1], sys.argv[-1])
else:
raise ValueError(f'unknown action {action}')