You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.4 KiB

4 years ago
  1. #!/home/alpcentaur/ProjektA/PrototypeWebApp/venv/bin/python3.5
  2. import sys
  3. import io
  4. import getopt
  5. from pdfminer.pdfinterp import PDFResourceManager, process_pdf
  6. from pdfminer.pdfdevice import TagExtractor
  7. from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter
  8. from pdfminer.layout import LAParams
  9. from pdfminer.utils import set_debug_logging
  10. def main(argv):
  11. def usage():
  12. print(('usage: %s [-d] [-p pagenos] [-m maxpages] [-P password] [-o output] [-C] '
  13. '[-n] [-A] [-V] [-M char_margin] [-L line_margin] [-W word_margin] [-F boxes_flow] '
  14. '[-Y layout_mode] [-O output_dir] [-t text|html|xml|tag] [-c codec] [-s scale] file ...' % argv[0]))
  15. return 100
  16. try:
  17. (opts, args) = getopt.getopt(argv[1:], 'dp:m:P:o:CnAVM:L:W:F:Y:O:t:c:s:')
  18. except getopt.GetoptError:
  19. return usage()
  20. if not args: return usage()
  21. debug = False
  22. # input option
  23. password = ''
  24. pagenos = set()
  25. maxpages = 0
  26. # output option
  27. outfile = None
  28. outtype = None
  29. outdir = None
  30. layoutmode = 'normal'
  31. codec = 'utf-8'
  32. pageno = 1
  33. scale = 1
  34. caching = True
  35. showpageno = True
  36. laparams = LAParams()
  37. for (k, v) in opts:
  38. if k == '-d': debug = True
  39. elif k == '-p': pagenos.update( int(x)-1 for x in v.split(',') )
  40. elif k == '-m': maxpages = int(v)
  41. elif k == '-P': password = v
  42. elif k == '-o': outfile = v
  43. elif k == '-C': caching = False
  44. elif k == '-n': laparams = None
  45. elif k == '-A': laparams.all_texts = True
  46. elif k == '-V': laparams.detect_vertical = True
  47. elif k == '-M': laparams.char_margin = float(v)
  48. elif k == '-L': laparams.line_margin = float(v)
  49. elif k == '-W': laparams.word_margin = float(v)
  50. elif k == '-F': laparams.boxes_flow = float(v)
  51. elif k == '-Y': layoutmode = v
  52. elif k == '-O': outdir = v
  53. elif k == '-t': outtype = v
  54. elif k == '-c': codec = v
  55. elif k == '-s': scale = float(v)
  56. if debug:
  57. set_debug_logging()
  58. rsrcmgr = PDFResourceManager(caching=caching)
  59. if not outtype:
  60. outtype = 'text'
  61. if outfile:
  62. if outfile.endswith('.htm') or outfile.endswith('.html'):
  63. outtype = 'html'
  64. elif outfile.endswith('.xml'):
  65. outtype = 'xml'
  66. elif outfile.endswith('.tag'):
  67. outtype = 'tag'
  68. if outfile:
  69. outfp = io.open(outfile, 'wt', encoding=codec, errors='ignore')
  70. close_outfp = True
  71. else:
  72. outfp = sys.stdout
  73. close_outfp = False
  74. if outtype == 'text':
  75. device = TextConverter(rsrcmgr, outfp, laparams=laparams)
  76. elif outtype == 'xml':
  77. device = XMLConverter(rsrcmgr, outfp, laparams=laparams, outdir=outdir)
  78. elif outtype == 'html':
  79. device = HTMLConverter(rsrcmgr, outfp, scale=scale, layoutmode=layoutmode,
  80. laparams=laparams, outdir=outdir, debug=debug)
  81. elif outtype == 'tag':
  82. device = TagExtractor(rsrcmgr, outfp)
  83. else:
  84. return usage()
  85. for fname in args:
  86. fp = io.open(fname, 'rb')
  87. process_pdf(rsrcmgr, device, fp, pagenos, maxpages=maxpages, password=password,
  88. caching=caching, check_extractable=True)
  89. fp.close()
  90. device.close()
  91. if close_outfp:
  92. outfp.close()
  93. if __name__ == '__main__':
  94. sys.exit(main(sys.argv))