-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwwc_TOC.py
508 lines (424 loc) · 18.8 KB
/
wwc_TOC.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# coding=latin-1
import fitz
from pathlib import Path
from utilities import openFile, getUniqueFileName
def max_depth(doc):
# returns max-depth of bookmarks
toc = doc.get_toc()
max_depth = 0
for t in toc:
level = t[0]
if level > max_depth: max_depth = level
return max_depth
def isTOC(doc, display=None):
# returns true if there is TOC (i.e. page labels with TOC)
for pg in range(0, doc.page_count):
page = doc[pg]
if isTOCPage(page): return True
return False
def isTOCPage(page):
if page.get_label()[:4] == "TOC_": return True
return False
def delete_toc(doc, options=None, display=None, addHistory=True):
# delete pages with pagelabel starting 'TOC'
if display: display.updatestatusBar("Deleting TOC...")
def adjusttoc(doc, no_pages):
def search_tree(node):
for child_node in display.tree.get_children(node):
pg = display.tree.item(child_node)['values'][2] - 1
if pg > 0: pg -= no_pages
pgLbl = doc[pg].get_label()
display.tree.set(child_node, 1, pgLbl)
display.tree.set(child_node, 2, pg + 1)
display.mirrorinChronoTree(child_node)
if display.tree.get_children(child_node):
search_tree(child_node)
search_tree("")
def getStartEndPage():
stPage = 0
enPage = 0
flag = False
for i in range(0, doc.page_count):
page = doc[i]
if isTOCPage(page):
flag = True
stPage = min(stPage, i)
enPage = max(enPage, i)
else:
if flag == True: break
if flag == True: return stPage, enPage
return None, None
no_pages = 0
stPage, enPage = getStartEndPage()
if not stPage == None and not enPage == None:
if display: display.updatestatusBar('About to delete pages')
doc.delete_pages(stPage, enPage)
no_pages = (enPage - stPage) + 1
if display: display.updatestatusBar('Deleted pages')
remove_toc_label(doc, no_pages)
adjusttoc(doc, no_pages)
for i in range(0, no_pages): display.dlist_tab.pop(0)
display.readTree()
if addHistory: display.adddocHistory({'code': 'DOC_removetoc'})
if display: display.updatestatusBar("Deleted TOC.")
return True
def remove_square_brackets(txt):
# removes square brackets from end of string
rBracket = txt.rfind(']')
if rBracket == len(txt) - 1:
lBracket = txt.rfind('[')
return txt[:lBracket].rstrip()
return txt
def print_pdf_string(doc, xref, indent=0):
keys = doc.xref_get_keys(xref)
for key in keys:
z = doc.xref_get_key(xref, key)
print(" " * indent, key, z)
if z[0] == 'xref':
indent += 1
print_pdf_string(doc, int(z[1][:-4]), indent)
pass
def write_chrono(**kwargs):
arrBkMks = kwargs['arrBkMks']
if not 'title' in kwargs: kwargs['title'] = 'Chronology'
if not 'margin' in kwargs: kwargs['margin'] = 25
if not 'folder' in kwargs: kwargs['folder'] = ''
if not 'day' in kwargs: kwargs['day'] = True
if not 'age' in kwargs: kwargs['age'] = True
kwargs['maxDepth'] = 6
font_size = 25
new_doc = fitz.open() # doc for storing toc
lnks = []
def breaktext(title, page, font_size, indent, width_pageref):
# returns list of lines to print for main text
lines = [] # stores each line of text
lines.append("") # add first line with empty string
title = title.strip() # remove leading and trailing spaces
words = title.split() # split into words
line = 0 # which line we are on
for word in words: # iterate each word
line_width = indent + fitz.get_text_length(text=lines[line] + word, fontsize=font_size)
if line_width > page.rect.width - kwargs['margin'] - width_pageref:
# then we put this word onto newline
lines.append("")
line += 1
lines[line] = word + " "
else:
# otherwise we just add to this line
lines[line] += word + " "
return lines
def add_page():
# adds a page
fmt = fitz.paper_rect("a4")
new_doc.new_page(width=fmt.width, height=fmt.height)
return new_doc[- 1]
total = len(arrBkMks)
indent = kwargs['margin']
y = 10
max_font_size = 20
page = add_page()
w = page.rect.width
h = page.rect.height
# The title
ls = breaktext('Chronology: ' + kwargs.get('title',''), page, font_size, indent, 0) # break long text into lines
for l in ls:
y += 1.2 * font_size
page.insert_text(point=(indent, y), text=l, fontsize=font_size) # the title
page.draw_line(p1=(kwargs['margin'], y + (0.2 * font_size)), p2=(w - kwargs['margin'], y + (0.2 * font_size)),
color=(0, 0, 0))
y += 10 # gap under contents line
count = 0
font_size = 10
old_day = None
flag_change_day = True
for bkmk in arrBkMks:
level = 0
# If change in day then flag
if old_day:
if old_day.date() != bkmk['date'].date():
flag_change_day = True
else:
flag_change_day = False
old_day = bkmk['date']
def format_date(bkmk):
dtStr = ''
dayOfWeek = ''
if kwargs['day']:
dayOfWeek = ' ' + bkmk['day'].ljust(2, ' ') + ' '
bkmk['indent'] = kwargs['margin'] + fitz.get_text_length(text="20/10/2020 Sa ", fontsize=font_size)
bkmk['indent_time'] = kwargs['margin'] + fitz.get_text_length(text="20/10/2020 Sa ", fontsize=font_size)
else:
bkmk['indent'] = kwargs['margin'] + fitz.get_text_length(text="20/10/2020 ", fontsize=font_size)
bkmk['indent_time'] = kwargs['margin'] + fitz.get_text_length(text="20/10/2020 ", fontsize=font_size)
if flag_change_day:
dtStr = bkmk['date'].strftime('%d/%m/%Y') + dayOfWeek
return dtStr
def format_time(bkmk):
dtStr = None
if bkmk['date'].hour == 0 and bkmk['date'].minute == 0 and bkmk['date'].second == 0 and bkmk[
'date'].microsecond == 0:
pass
else:
if bkmk['date'].second == 0:
dtStr = bkmk['date'].strftime('%H:%M')
else:
dtStr = bkmk['date'].strftime('%H:%M:%S')
return dtStr
# title_date=format_date(bkmk)
# title_description = bkmk['description']
pg = bkmk['page']
if level <= kwargs['maxDepth']:
# get page label
if pg > -1:
pg_label = bkmk['label']
else:
pg_label = 'n/a' # bookmark doesn't point to valid page
# Set font size and colour depending on level
if level == 1:
font_size = max_font_size - 10
colour = bkmk['color']
indent = kwargs['margin']
else:
font_size = max_font_size - 10
colour = bkmk['color']
indent = kwargs['margin'] + 10 * level
width_pageref = fitz.get_text_length(text=pg_label, fontsize=font_size)
def get_max_x_cursor(x_cursor_max):
x_cursor_max += fitz.get_text_length(text="20/10/2020 ", fontsize=font_size)
if kwargs['day']:
x_cursor_max += fitz.get_text_length(text=" Th ", fontsize=font_size)
if kwargs['age']:
x_cursor_max += fitz.get_text_length(text=" 100.0 ", fontsize=font_size)
if format_time(bkmk):
x_cursor_max += fitz.get_text_length(text="00:00:00 ", fontsize=font_size)
return x_cursor_max
ls = breaktext(bkmk['description'], page, font_size, get_max_x_cursor(kwargs['margin']),
width_pageref) # break long text into lines
if (y + len(ls) * font_size) + (len(ls) - 1) * (0.2 * font_size) + kwargs['margin'] > h:
page = add_page() # add page if needed
y = kwargs['margin']
x_cursor = kwargs['margin'] # start at the margin
# insert the date
if flag_change_day:
page.insert_text(point=(x_cursor, y + 1.2 * font_size), text=bkmk['dt_txt'], fontsize=font_size,
color=bkmk['color']) # insert date
x_cursor += fitz.get_text_length(text="20/10/2020 ", fontsize=font_size)
if kwargs['day']:
page.insert_text(point=(x_cursor, y + 1.2 * font_size), text=bkmk['day'], fontsize=font_size,
color=bkmk['color']) # insert day
x_cursor += fitz.get_text_length(text=" Th ", fontsize=font_size)
if kwargs['age']:
page.insert_text(point=(x_cursor, y + 1.2 * font_size), text="{:.1f}".format(bkmk['age']),
fontsize=font_size,
color=bkmk['color']) # insert day
x_cursor += fitz.get_text_length(text=" 100.0 ", fontsize=font_size)
# insert the time
if format_time(bkmk):
page.insert_text(point=(x_cursor, y + 1.2 * font_size), text=format_time(bkmk), fontsize=font_size,
color=bkmk['color']) # insert time
x_cursor += fitz.get_text_length(text="00:00:00 ", fontsize=font_size)
# insert the description over each line
for l in ls:
y += 1.2 * font_size # move a line down
page.insert_text(point=(x_cursor, y), text=l, fontsize=font_size,
color=bkmk['color']) # insert main text
width_text = fitz.get_text_length(text=l, fontsize=font_size)
# insert the dots
page.draw_line(p1=(x_cursor + width_text + 2, y - 0.25 * font_size * 1.2),
p2=(w - kwargs['margin'] - width_pageref - 2, y - 0.25 * font_size * 1.2),
color=bkmk['color'],
dashes="[3] 0") # insert dot dot dot
# insert the page label
page.insert_text(point=((w - kwargs['margin']) - width_pageref, y), text=pg_label, fontsize=font_size,
color=bkmk['color']) # insert page label
# add link
stY = y - (len(ls) - 1) * (1.2 * font_size)
# pg = pg-1
# LINK_GOTOR is link to place in another pdf
if pg >= -1:
#TODO: use named destinations as an alternative to page labels
l = {'kind': fitz.LINK_GOTOR,
'from': fitz.Rect(bkmk['indent'], stY - 0.5 * font_size * 1.2, w - kwargs['margin'], y),
'page': pg, 'zoom': 0.0, 'file': bkmk['file'], 'id': '', 'NewWindow': True}
lnks.append({'pgNo': page.number, 'link': l})
count += 1
percentComplete = (float(count) / float(total)) * 1000
page=add_page() #apparently needed to trigger first_link on previous page
# add TOC labels
for l in lnks:
print(l['pgNo'])
new_doc[l['pgNo']].insert_link(l['link'])
for pg in new_doc:
lnks = pg.get_links()
for l in lnks:
new_doc.xref_set_key(l['xref'], 'A/NewWindow', 'true')
new_doc.xref_set_key(l['xref'], 'A/D', f"[{l['page']}/Fit]")
new_doc.delete_page(new_doc.page_count-1)
chronoFile = Path(kwargs['folder']) / 'Chronology.pdf'
uchronoFile = getUniqueFileName(chronoFile)
new_doc.save(uchronoFile)
new_doc.close()
openFile(uchronoFile)
print('finished')
return True
def write_toc(doc, options, display=None, addHistory=True):
if not 'margin' in options: options['margin'] = 25
if options['maxDepth'] > doc.max_depth(): options['maxDepth'] = doc.max_depth()
new_doc = fitz.open() # doc for storing toc
lnks = []
if display: display.updatestatusBar("Adding TOC...")
def breaktext(title, page, font_size, indent, width_pageref):
# returns list of lines to print for main text
lines = [] # stores each line of text
lines.append("") # add first line with empty string
title = title.strip() # remove leading and trailing spaces
words = title.split() # split into words
line = 0 # which line we are on
for word in words: # iterate each word
line_width = indent + fitz.get_text_length(text=lines[line] + word, fontsize=font_size)
if line_width > page.rect.width - options['margin'] - width_pageref:
# then we put this word onto newline
lines.append("")
line += 1
lines[line] = word + " "
else:
# otherwise we just add to this line
lines[line] += word + " "
return lines
def add_page():
# adds a page
fmt = fitz.paper_rect("a4")
new_doc.new_page(width=fmt.width, height=fmt.height)
return new_doc[- 1]
if isTOC(doc): delete_toc(doc, options, display, addHistory=False)
toc = doc.get_toc()
total = len(toc)
indent = options['margin']
y = 10
max_font_size = 20
font_size = 25
page = add_page()
w = page.rect.width
h = page.rect.height
# The title
ls = breaktext(options['title'], page, font_size, indent, 0) # break long text into lines
for l in ls:
y += 1.2 * font_size
page.insert_text(point=(indent, y), text=l, fontsize=font_size) # the title
page.draw_line(p1=(options['margin'], y + (0.2 * font_size)), p2=(w - options['margin'], y + (0.2 * font_size)),
color=(0, 0, 0))
y += 10 # gap under contents line
count = 0
for t in toc:
level = t[0]
title = remove_square_brackets(t[1])
pg = t[2]
if level <= options['maxDepth']:
# get page label
if pg > -1:
pg_label = display.doc[t[2] - 1].get_label()
else:
pg_label = 'n/a' # bookmark doesn't point to valid page
# Set font size and colour depending on level
if level == 1:
font_size = max_font_size
colour = (0, 0, 1)
indent = options['margin']
else:
font_size = max_font_size - (2 * level)
colour = (0, 0, 0)
indent = options['margin'] + 10 * level
width_pageref = fitz.get_text_length(text=pg_label, fontsize=font_size)
ls = breaktext(title, page, font_size, indent, width_pageref) # break long text into lines
if (y + len(ls) * font_size) + (len(ls) - 1) * (0.2 * font_size) + options['margin'] > h:
page = add_page() # add page if needed
y = options['margin']
for l in ls:
y += 1.2 * font_size
page.insert_text(point=(indent, y), text=l, fontsize=font_size, color=colour) # insert main text
width_text = fitz.get_text_length(text=l, fontsize=font_size)
page.draw_line(p1=(indent + width_text + 2, y - 0.25 * font_size * 1.2),
p2=(w - options['margin'] - width_pageref - 2, y - 0.25 * font_size * 1.2), color=colour,
dashes="[3] 0")
page.insert_text(point=((w - options['margin']) - width_pageref, y), text=pg_label, fontsize=font_size,
color=colour) # insert page label
# add link
stY = y - (len(ls) - 1) * (1.2 * font_size)
pg = t[2] - 1
if not pg == -1:
l = {'kind': 1, 'from': fitz.Rect(indent, stY - 0.5 * font_size * 1.2, w - options['margin'], y),
type: 'goto', 'page': pg, 'nflink': True, 'zoom': 0.0}
lnks.append({'pgNo': page.number, 'link': l})
count += 1
percentComplete = (float(count) / float(total)) * 100
if int(percentComplete) % 10 == 0:
if display: display.updateprogressBar(percentComplete)
# add TOC labels
_add_toc_label(new_doc)
display.insertPages(new_doc, 0)
for l in lnks:
l['link']['page'] += new_doc.page_count
doc[l['pgNo']].insert_link(l['link'])
new_doc.close()
display.readTree()
if addHistory: display.adddocHistory({'code': 'DOC_addtoc', 'options': options})
if display: display.updatestatusBar('Finished TOC.')
return True
def _add_toc_label(new_doc):
labels = []
labels.append({'startpage': 0, 'prefix': 'TOC_', 'style': 'r', 'firstpagenum': 1})
new_doc.set_page_labels(labels)
def remove_toc_label(doc, no_pages):
# remove toc_label and update the rest
labels = doc.get_labels_rule_dict()
new_labels = []
for label in labels:
if not label['prefix'][:3] == 'TOC': # skip TOC label
label['startpage'] = label['startpage'] - no_pages # adjust
new_labels.append(label)
doc.set_page_labels(new_labels)
def add_toc_label(doc, no_pages):
# adds toc label
new_label = {'startpage': 0, 'prefix': 'TOC_', 'style': 'r', 'firstpagenum': 1}
labels = doc.get_labels_rule_dict()
# increment startpage all by no_pages
if len(labels) == 0: # if no labels create default one
labels.append({'startpage': 0, 'prefix': '', 'style': 'D', 'firstpagenum': 1})
for label in labels:
label['startpage'] = label['startpage'] + no_pages
labels.insert(0, new_label)
doc.set_page_labels(labels)
def change_pdf_string(doc, xref, indent=0):
keys = doc.xref_get_keys(xref)
for key in keys:
z = doc.xref_get_key(xref, key)
# print(" " * indent,key, z)
if key == 'F':
doc.xref_set_key(xref, "NewWindow", 'true')
z = doc.xref_get_key(xref, "NewWindow")
# print(" "*indent,"New: ","NewWindow",z)
if z[0] == 'xref':
indent += 1
change_pdf_string(doc, int(z[1][:-4]), indent)
def print_pdf_string_(doc, xref, indent=0):
keys = doc.xref_get_keys(xref)
for key in keys:
z = doc.xref_get_key(xref, key)
print(" " * indent, key, z)
if z[0] == 'xref':
indent += 1
print_pdf_string_(doc, int(z[1][:-4]), indent)
def print_pdf_string(doc, xref, indent=0):
print(doc.xref_object(xref))
def test():
doc = fitz.open("C:/Users/samsu/Dropbox/My documents/WWC Pigeon Hole/TEST LINK.pdf")
page = doc[0]
lnks = page.get_links()
for lnk in lnks:
change_pdf_string(doc, lnk['xref'])
print_pdf_string_(doc, lnk['xref'])
# print_pdf_string(doc,lnk['xref'])
print(print_pdf_string_(doc, 99))
doc.saveIncr()
if __name__ == "__main__":
test()