-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtotal_pdf_pages_in_tar_gz.py
46 lines (38 loc) · 1.13 KB
/
total_pdf_pages_in_tar_gz.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
import tarfile
from pdfrw import PdfReader
def total_pdf_pages_in_tar_gz(tfn):
tar = tarfile.open(tfn, "r:gz")
errors = {}
pages = {}
for i, tarinfo in enumerate(tar):
name = tarinfo.name
name = name.split('/')[-1]
if tarinfo.isreg():
member = tar.getmember(tarinfo.name)
try:
f = tar.extractfile(member)
pdf = PdfReader(f)
count = len(pdf.pages)
pages[name] = count
except Exception as e:
pages[name] = 0
msg = e.msg
if msg in errors:
errors[msg] += 1
else:
errors[msg] = 1
finally:
f.close()
elif tarinfo.isdir():
pass
else:
print("something else.")
return (pages, errors)
if __name__ == "__main__":
files, errors = total_pdf_pages_in_tar_gz("sample_pdfs.tar.gz")
print("files:", len(files))
print("errors:", errors)
count = 0
for p in files:
count += files[p]
print("pages: {:d}".format(count))