2013-10-17 14:05:27 +00:00
|
|
|
#!/usr/bin/env python
|
2009-05-15 14:34:53 +00:00
|
|
|
import sys
|
2013-10-10 09:29:30 +00:00
|
|
|
from pdfminer.pdfdocument import PDFDocument
|
|
|
|
from pdfminer.pdfparser import PDFParser
|
2009-05-16 06:12:01 +00:00
|
|
|
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter, process_pdf
|
2010-04-24 13:31:21 +00:00
|
|
|
from pdfminer.pdfdevice import PDFDevice, TagExtractor
|
|
|
|
from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter
|
2009-12-19 14:17:00 +00:00
|
|
|
from pdfminer.cmapdb import CMapDB
|
2009-07-21 07:55:19 +00:00
|
|
|
from pdfminer.layout import LAParams
|
2011-07-18 11:07:00 +00:00
|
|
|
from pdfminer.image import ImageWriter
|
2009-05-15 14:34:53 +00:00
|
|
|
|
|
|
|
# main
|
|
|
|
def main(argv):
|
2009-10-24 04:41:59 +00:00
|
|
|
import getopt
|
|
|
|
def usage():
|
2011-03-02 15:04:43 +00:00
|
|
|
print ('usage: %s [-d] [-p pagenos] [-m maxpages] [-P password] [-o output] [-C] '
|
2011-02-02 14:09:34 +00:00
|
|
|
'[-n] [-A] [-V] [-M char_margin] [-L line_margin] [-W word_margin] [-F boxes_flow] '
|
2010-12-26 08:26:39 +00:00
|
|
|
'[-Y layout_mode] [-O output_dir] [-t text|html|xml|tag] [-c codec] [-s scale] file ...' % argv[0])
|
2009-10-24 04:41:59 +00:00
|
|
|
return 100
|
|
|
|
try:
|
2011-03-02 15:04:43 +00:00
|
|
|
(opts, args) = getopt.getopt(argv[1:], 'dp:m:P:o:CnAVM:L:W:F:Y:O:t:c:s:')
|
2009-10-24 04:41:59 +00:00
|
|
|
except getopt.GetoptError:
|
|
|
|
return usage()
|
|
|
|
if not args: return usage()
|
|
|
|
# debug option
|
|
|
|
debug = 0
|
|
|
|
# input option
|
|
|
|
password = ''
|
|
|
|
pagenos = set()
|
|
|
|
maxpages = 0
|
|
|
|
# output option
|
|
|
|
outfile = None
|
|
|
|
outtype = None
|
2011-07-18 11:07:00 +00:00
|
|
|
imagewriter = None
|
2010-11-14 15:04:28 +00:00
|
|
|
layoutmode = 'normal'
|
2009-10-24 04:41:59 +00:00
|
|
|
codec = 'utf-8'
|
|
|
|
pageno = 1
|
|
|
|
scale = 1
|
2011-03-02 15:04:43 +00:00
|
|
|
caching = True
|
2009-10-24 04:41:59 +00:00
|
|
|
showpageno = True
|
|
|
|
laparams = LAParams()
|
|
|
|
for (k, v) in opts:
|
|
|
|
if k == '-d': debug += 1
|
|
|
|
elif k == '-p': pagenos.update( int(x)-1 for x in v.split(',') )
|
|
|
|
elif k == '-m': maxpages = int(v)
|
2010-03-22 08:36:39 +00:00
|
|
|
elif k == '-P': password = v
|
2009-10-24 04:41:59 +00:00
|
|
|
elif k == '-o': outfile = v
|
2011-03-02 15:04:43 +00:00
|
|
|
elif k == '-C': caching = False
|
2009-11-07 09:12:54 +00:00
|
|
|
elif k == '-n': laparams = None
|
2010-04-10 11:30:03 +00:00
|
|
|
elif k == '-A': laparams.all_texts = True
|
2011-02-02 14:09:34 +00:00
|
|
|
elif k == '-V': laparams.detect_vertical = True
|
2009-10-24 04:41:59 +00:00
|
|
|
elif k == '-M': laparams.char_margin = float(v)
|
|
|
|
elif k == '-L': laparams.line_margin = float(v)
|
|
|
|
elif k == '-W': laparams.word_margin = float(v)
|
2010-12-26 08:26:39 +00:00
|
|
|
elif k == '-F': laparams.boxes_flow = float(v)
|
2010-11-14 15:04:28 +00:00
|
|
|
elif k == '-Y': layoutmode = v
|
2011-07-18 11:07:00 +00:00
|
|
|
elif k == '-O': imagewriter = ImageWriter(v)
|
2010-03-22 08:36:39 +00:00
|
|
|
elif k == '-t': outtype = v
|
|
|
|
elif k == '-c': codec = v
|
|
|
|
elif k == '-s': scale = float(v)
|
2009-10-24 04:41:59 +00:00
|
|
|
#
|
2011-04-21 13:07:52 +00:00
|
|
|
PDFDocument.debug = debug
|
|
|
|
PDFParser.debug = debug
|
2009-10-24 04:41:59 +00:00
|
|
|
CMapDB.debug = debug
|
|
|
|
PDFResourceManager.debug = debug
|
|
|
|
PDFPageInterpreter.debug = debug
|
|
|
|
PDFDevice.debug = debug
|
|
|
|
#
|
2011-03-02 15:04:43 +00:00
|
|
|
rsrcmgr = PDFResourceManager(caching=caching)
|
2009-10-24 04:41:59 +00:00
|
|
|
if not outtype:
|
|
|
|
outtype = 'text'
|
|
|
|
if outfile:
|
|
|
|
if outfile.endswith('.htm') or outfile.endswith('.html'):
|
|
|
|
outtype = 'html'
|
2009-10-31 03:04:56 +00:00
|
|
|
elif outfile.endswith('.xml'):
|
|
|
|
outtype = 'xml'
|
2009-10-24 04:41:59 +00:00
|
|
|
elif outfile.endswith('.tag'):
|
|
|
|
outtype = 'tag'
|
2009-06-20 10:00:51 +00:00
|
|
|
if outfile:
|
2009-10-24 04:41:59 +00:00
|
|
|
outfp = file(outfile, 'w')
|
|
|
|
else:
|
|
|
|
outfp = sys.stdout
|
|
|
|
if outtype == 'text':
|
2011-11-06 15:15:10 +00:00
|
|
|
device = TextConverter(rsrcmgr, outfp, codec=codec, laparams=laparams,
|
|
|
|
imagewriter=imagewriter)
|
2009-10-31 03:04:56 +00:00
|
|
|
elif outtype == 'xml':
|
2011-07-18 11:07:00 +00:00
|
|
|
device = XMLConverter(rsrcmgr, outfp, codec=codec, laparams=laparams,
|
|
|
|
imagewriter=imagewriter)
|
2009-10-24 04:41:59 +00:00
|
|
|
elif outtype == 'html':
|
2010-11-14 15:04:28 +00:00
|
|
|
device = HTMLConverter(rsrcmgr, outfp, codec=codec, scale=scale,
|
2011-07-18 11:07:00 +00:00
|
|
|
layoutmode=layoutmode, laparams=laparams,
|
|
|
|
imagewriter=imagewriter)
|
2009-10-24 04:41:59 +00:00
|
|
|
elif outtype == 'tag':
|
2010-04-24 04:32:03 +00:00
|
|
|
device = TagExtractor(rsrcmgr, outfp, codec=codec)
|
2009-10-24 04:41:59 +00:00
|
|
|
else:
|
|
|
|
return usage()
|
|
|
|
for fname in args:
|
|
|
|
fp = file(fname, 'rb')
|
2010-11-23 10:53:28 +00:00
|
|
|
process_pdf(rsrcmgr, device, fp, pagenos, maxpages=maxpages, password=password,
|
2011-03-02 15:04:43 +00:00
|
|
|
caching=caching, check_extractable=True)
|
2009-10-24 04:41:59 +00:00
|
|
|
fp.close()
|
|
|
|
device.close()
|
2009-10-31 02:09:36 +00:00
|
|
|
outfp.close()
|
2009-10-24 04:41:59 +00:00
|
|
|
return
|
2009-05-15 14:34:53 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__': sys.exit(main(sys.argv))
|