-
Notifications
You must be signed in to change notification settings - Fork 142
/
list.py
77 lines (66 loc) · 1.63 KB
/
list.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
"""
Print a list of embedded files in a document
-------------------------------------------------------------------------------
License: GNU AGPL V3
(c) 2021 Jorj X. McKie
Usage
-----
python list.py input.pdf
Notes
-----
The output.pdf file generated in examples/embed-images is renamed as input.pdf
to be used as the input file in this example.
Dependencies
------------
PyMuPDF
"""
from __future__ import print_function
import sys
import fitz
fn = sys.argv[1]
doc = fitz.open(fn)
name_len = filename_len = 0
total_len = total_size = 0
ef_list = []
for i in range(doc.embfile_count()):
info = doc.embfile_info(i)
ef = (
info["name"],
info["filename"],
info["length"],
info["size"],
)
ef_list.append(ef)
name_len = max(len(ef[0]), name_len)
filename_len = max(len(ef[1]), filename_len)
total_len += ef[2]
total_size += ef[3]
if len(ef_list) < 1:
print("no embedded files in", fn)
exit(1)
ratio = float(total_size) / total_len
saves = 1 - ratio
header = (
"Name".ljust(name_len + 4)
+ "Filename".ljust(filename_len + 4)
+ "Length".rjust(10)
+ "Size".rjust(11)
)
line = "-".ljust(len(header), "-")
print(line)
print(header)
print(line)
for info in ef_list:
print(
info[0].ljust(name_len + 3),
info[1].ljust(filename_len + 3),
str(info[2]).rjust(10),
str(info[3]).rjust(10),
)
print(line)
print(len(ef_list), "embedded files in '%s'. Totals:" % (fn,))
print(
"File lengths: %s, compressed: %s, ratio: %s%% (savings: %s%%)."
% (total_len, total_size, str(round(ratio * 100, 2)), str(round(saves * 100, 2)))
)
print(line)