-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaip.py
154 lines (129 loc) · 5 KB
/
aip.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/env python
#
# Copyright (C) 2021 Mario Haustein, mario@mariohaustein.de
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
import pikepdf
def load_pages(pdfs):
pdfs = list(pdfs)
result = []
expectpdf = True
while pdfs:
pdfname = pdfs.pop(0)
if not pdfname.endswith(".pdf"):
raise ValueError("'%s' ist kein gültiger Dateiname." % pdfname)
pdf = pikepdf.Pdf.open(pdfname)
if len(pdfs) == 0 or pdfs[0].endswith(".pdf"):
# Wir müssen das PDF-Objekt zurückgeben, auch wenn wir es nicht
# benötigen. Andernfalls würde die letzte Referenz darauf bei
# Rückkehr aus der Funktion aufgegeben. Der Garbace Collector darf
# das Objekt löschen und der Zugriff auf Objekte in
# `list(pdf.pages)` schlagen aufgrund der Implementierung von
# pikepdf fehl.
result.append(( pdfname, list(pdf.pages), pdf ))
continue
resultpages = []
pagespec = pdfs.pop(0)
for ps in pagespec.split(","):
if ps == "{}":
resultpages.append(None)
continue
prange = ps.split("-")
if len(prange) > 2:
raise ValueError(
"Ungültiger Bereich '%s' für '%s'. '%s' ist keine Bereichsangabe." %
(
pagespec,
pdfname,
ps,
)
)
if prange[0].isnumeric():
pfrom = int(prange[0])
elif prange[0] == '':
pfrom = None
else:
raise ValueError(
"Ungültiger Bereich '%s' für '%s'. '%s' ist keine Seiten-/Bereichsangabe." %
(
pagespec,
pdfname,
ps,
)
)
if len(prange) < 2:
pto = None
elif prange[1].isnumeric():
pto = int(prange[1])
elif prange[1] == '':
pto = None
else:
raise ValueError(
"Ungültiger Bereich '%s' für '%s'. '%s' ist keine Bereichsangabe." %
(
pagespec,
pdfname,
ps,
)
)
if pfrom is not None and (pfrom < 1 or pfrom > len(pdf.pages)):
raise ValueError(
"Ungültiger Bereich '%s' für '%s'. Seitenangabe '%d' außerhalb des Bereichs. Das PDF enthält %d Seiten." %
(
pagespec,
pdfname,
pfrom,
len(pdf.pages),
)
)
if pto is not None and (pto < 1 or pto > len(pdf.pages)):
raise ValueError(
"Ungültiger Bereich '%s' für '%s'. Seitenangabe '%d' außerhalb des Bereichs. Das PDF enthält %d Seiten." %
(
pagespec,
pdfname,
pto,
len(pdf.pages),
)
)
if pfrom is not None and pto is not None and pfrom > pto:
raise ValueError(
"Ungültiger Bereich '%s' für '%s'. Bereichsangabe '%s' ist nicht aufsteigend." %
(
pagespec,
pdfname,
ps,
)
)
if len(prange) == 1:
if pfrom is None:
raise ValueError(
"Ungültiger Bereich '%s' für '%s'. Bereich enthält leere Seiten-/Bereichsangabe." %
(
pagespec,
pdfname,
)
)
resultpages.append(pdf.pages[pfrom - 1])
continue
if pfrom is None:
pfrom = 1
if pto is None:
pto = len(pdf.pages)
for p in pdf.pages[pfrom - 1 : pto]:
resultpages.append(p)
result.append(( pdfname, resultpages, pdf ))
return result