-
Notifications
You must be signed in to change notification settings - Fork 0
/
contents.py
executable file
·327 lines (243 loc) · 10.1 KB
/
contents.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# -*- coding: utf-8 -*-
"""
Small Python module to format the table of contents for inclusion in a
PDF or DJVU file.
# Initial contents file formating
The input file 'contents.txt' must be formated this way:
- number of spaces at the beginning of the line indicates the level;
- the number at the end of the line indicates the page;
- (blank lines are ignored);
Output defaults to 'contents.bmk'.
One liner, provided the contents is written in 'contents.txt':
> from contents import Contents
> Contents().write4PDFTK()
# Use of output contents file
## DJVU files
Use the two following commands:
> djvused -e print-outline book.djvu
> djvused -s -e 'set-outline contents.bmk' book.djvu
## PDF files
First, get the metadata of the PDF file:
> pdftk file.pdf dump_data > metadata.txt
Then, modify the metadata file by including contents.bmk after the line containing
the keyword "NumberOfPages:".
Finally, updade the PDf metadata:
> pdftk file.pdf update_info metadata.txt output newfile.pdf
Created on Thu Aug 20 13:20:10 2020
@author: dbrizard
"""
import os
import subprocess as sbp
import glob
import warnings
class Contents(object):
"""
"""
def __init__(self, fname='contents.txt', pagesep=' ', indent=' ', debug=False):
"""
:param str fname: file name
:param str pagesep: page number separator
:param str indent: indentation (level) separator
:param bool debug: print lines for debugging purpose
"""
with open(fname, 'r') as f:
text = f.readlines()
self.text = text # [xx.strip() for xx in text]
self.pagesep = pagesep
self.indent = indent
self.detectOffsetOpen()
self.treatLines(debug=debug)
def detectOffsetOpen(self):
"""Automatic detection of page number offset, written as first line.
Also scans first line for 'open' or 'close' keyword for bookmarks
Warning: 'close' option must be BEFORE 'offset' option
"""
fline = self.text[0] # fisrt line
if "open" in fline.lower():
self.Open = True
print("Open bookmarks option detected in first line.")
fline = fline.replace("open", "")
elif "close" in fline.lower():
self.Open = False
print("Close bookmarks option detected in first line.")
fline = fline.replace("close","")
if "offset" in fline:
# remove trailing \n
fline.rstrip()
ind = fline.rfind(self.pagesep)
self.offset = int( fline[ind+len(self.pagesep):] )
print("Offset detected in first line: %i"%self.offset)
# XXX offset option deprecated, + or - line should be prefered
else:
self.offset = 0 #None
if hasattr(self, "Open") or "offset" in fline.lower():
# remove offset line
self.text.pop(0)
# def arbitrateOffset(self, offset):
# """
# :param int offset: offset given while calling write4XXX method
# """
# if offset is None and self.offset is not None:
# final_offset = self.offset
# elif offset is not None and self.offset is None:
# final_offset = offset
# elif offset is not None and self.offset is not None:
# final_offset = self.offset
# elif offset is None and self.offset is None:
# final_offset = 0
# return final_offset
def treatLines(self, debug=False):
"""Read each line and get title, level (indentation) and page number
:param bool debug: print lines for debugging purpose
"""
level = []
title = []
page = []
offset = self.offset
for ll in self.text:
#---Remove trailing \n---
lll = ll.rstrip()
if debug:
print(lll)
if not len(lll)==0:
if lll[0]=='+':
# Positive offset line
print("Positive offset: +%s"%lll[1:])
offset += int(lll[1:])
elif lll[0]=='-':
# Negative offset line
print("Negative offset: -%s"%lll[1:])
offset -= int(lll[1:])
else:
# Regular content line (title, page number)
#---Count number of leading sep---
ni = len(lll) - len(lll.lstrip(self.indent))
#---Get page number---
ind = lll.rfind(self.pagesep)
pagenumb = lll[ind+len(self.pagesep):]
#---Get title---
ttl = lll[:ind]
nonumber = False
try:
page.append(int(pagenumb)+offset)
fail = True
except ValueError:
print("No page number: %s"%lll)
nonumber = True
if not nonumber:
level.append(ni+1)
title.append(ttl.strip())
self.level = level
self.page = page
self.title = title
def write4PDFTK(self, fname='contents.bmk', offset=0):
"""
:param str fname: output filename
:param int offset: page number offset
"""
# offset = self.arbitrateOffset(offset)
with open(fname, 'w') as f:
for tt, ll, pp in zip(self.title, self.level, self.page):
# pp = pp + offset
f.write('BookmarkBegin\n')
f.write('BookmarkTitle: %s\n'%tt)
f.write('BookmarkLevel: %i\n'%ll)
f.write('BookmarkPageNumber: %i\n'%pp)
def write4CPDF(self, fname='contents.bmk', offset=0, Open=True):
"""
:param str fname: output filename
:param int offset: page number offset
:param bool Open: children visible or not when pdf file loaded.
"""
if hasattr(self, "Open"):
Open = self.Open
# offset = self.arbitrateOffset(offset)
with open(fname, 'w') as f:
for tt, ll, pp in zip(self.title, self.level, self.page):
# pp = pp + offset
if '"' in tt:
print('/!\ found and replaced illegal " character in string in:')
print(tt)
#print('=> Consider using “…” or «…»')
tt = tt.replace('"', '“')
if Open:
line = '%i "%s" %i open\n'%(ll-1, tt, pp)
else:
line = '%i "%s" %i\n'%(ll-1, tt, pp)
f.write(line)
def write4DJVU(self, fname='contents.bmk', offset=0):
"""
:param str fname: output filename
:param int offset: page number offset
"""
# offset = self.arbitrateOffset(offset)
n = len(self.title)
with open(fname, 'w') as f:
f.write('(bookmarks\n')
for ii, (tt, ll, pp) in enumerate(zip(self.title, self.level, self.page)):
# pp = pp + offset
if '"' in tt:
print('/!\ found and replaced illegal " character in string in:')
print(tt)
#print('=> Consider using “…” or «…»')
tt = tt.replace('"', '“')
if ii<n-1:
if self.level[ii+1]==self.level[ii]:
# next is the same level
f.write('("%s" "#%i")\n'%(tt, pp))
elif self.level[ii+1]>self.level[ii]:
# there will be sublevels, keep parenthesis open
f.write('("%s" "#%i"\n'%(tt, pp))
elif self.level[ii+1]<self.level[ii]:
# no more sublevels, close parentheses
n_close = self.level[ii]-self.level[ii+1] +1
f.write('("%s" "#%i"'%(tt, pp)+' )'*n_close +'\n')
else:
n_close = ll + 1
f.write('("%s" "#%i"'%(tt, pp)+' )'*n_close +'\n')
def addPDFtoc(pdffile=None):
"""
"""
warnings.warn("This is not working yet..")
#---GET PDF FILE---
if pdffile is None:
pdflist = glob.glob("*.pdf")
if not len(pdflist)==0:
if len(pdflist)>1:
print("Several PDF files...")
print(pdflist)
pdffile = pdflist[0]
print()
else:
print("/!\ no pdf files in the folder...")
#---GET META DATA---
sbp.call(['pdftk %s dump_data meta.txt'%pdffile])
#---SUPPOSE CONTENTS.BMK IS HERE---
Contents().writePDFcontents()
with open("contents.bmk") as f:
cont = f.read()
#---MODIFY META.TXT---
with open('meta.txt', 'r') as f:
meta = f.readlines()
# find line with "NumberOfPages"
for ii, ll in enumerate(meta[:20]):
if 'NumberOfPages' in ll:
ind = ii
# write new meta.txt file
meta.insert(ind, cont)
meta2 = "".join(meta)
# https://stackoverflow.com/questions/10507230/insert-line-at-middle-of-file-with-python
with open("meta2.txt", 'r') as f:
f.write(meta2)
#---ADD CONTENTS TO PDF FILE---
sbp.call(['pdftk', pdffile, 'update_info meta2.txt output test.pdf'])
if __name__=='__main__':
#%% TEST CONTENTS
C = Contents('contents.txt', debug=False) # XXX change the example file
C.write4CPDF('example_output/contents_cpdf.bmk')
C.write4PDFTK('example_output/contents_pdftk.bmk')
C.write4DJVU('example_output/contents_djvu.bmk')
#%%TEST ADDPDFTOC---
if False:
print("TTTTTTTTTTTTTT")
addPDFtoc() # XXX not finished at all