forked from spyysalo/taggedpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfstruct.py
More file actions
executable file
·65 lines (51 loc) · 1.58 KB
/
Copy pathpdfstruct.py
File metadata and controls
executable file
·65 lines (51 loc) · 1.58 KB
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
#!/usr/bin/env python3
# Output logical structure of tagged PDF files. Follows in part the
# implementation of `pdfinfo -struct` from poppler-utils.
import sys
from io import StringIO
from argparse import ArgumentParser
from taggedpdf import TaggedPdf, OutputFormat
from taggedpdf.utils import check_xml
from taggedpdf.logger import logger
def argparser():
ap = ArgumentParser()
formats = [f.value for f in OutputFormat]
ap.add_argument('pdf', nargs='+')
ap.add_argument(
'--format',
choices=formats,
default=formats[0],
help='output format'
)
ap.add_argument(
'--skip-check',
default=False,
action='store_true',
help='do not check that output is valid XML (with XML format)'
)
return ap
def output_pdf_struct(fn, args):
pdf = TaggedPdf(fn)
if pdf.struct_tree_root is None:
logger.info(f'{fn}: no structure tree')
return
root = pdf.struct_tree_root
if args.skip_check or args.format != 'xml':
root.write_struct_tree(fmt=OutputFormat(args.format))
else:
assert args.format == 'xml'
output = StringIO()
root.write_struct_tree(fmt=OutputFormat(args.format), out=output)
output = output.getvalue()
check_xml(output)
print(output, end='')
def main(argv):
args = argparser().parse_args(argv[1:])
for fn in args.pdf:
try:
output_pdf_struct(fn, args)
except Exception as e:
logger.error(f'{fn}: {e}')
raise
if __name__ == '__main__':
sys.exit(main(sys.argv))