2009-05-15 14:34:53 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import sys
|
2009-05-16 06:12:01 +00:00
|
|
|
from pdfminer.pdfparser import PDFDocument, PDFParser
|
|
|
|
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter, process_pdf
|
2009-05-16 10:42:35 +00:00
|
|
|
from pdfminer.pdfdevice import PDFDevice
|
2009-10-31 03:04:56 +00:00
|
|
|
from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter, TagExtractor
|
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
|
2009-05-15 14:34:53 +00:00
|
|
|
|
|
|
|
# main
|
|
|
|
def main(argv):
|
2009-10-24 04:41:59 +00:00
|
|
|
import getopt
|
|
|
|
def usage():
|
2010-03-22 08:36:39 +00:00
|
|
|
print ('usage: %s [-d] [-p pagenos] [-m maxpages] [-P password] [-o output] '
|
2009-11-07 09:12:54 +00:00
|
|
|
'[-n] [-D direction] [-M char_margin] [-L line_margin] [-W word_margin] '
|
2010-03-22 08:36:39 +00:00
|
|
|
'[-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:
|
2010-03-22 08:36:39 +00:00
|
|
|
(opts, args) = getopt.getopt(argv[1:], 'dp:m:P:o:nD:M:L:W: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
|
2010-01-31 02:09:28 +00:00
|
|
|
outdir = None
|
2009-10-24 04:41:59 +00:00
|
|
|
codec = 'utf-8'
|
|
|
|
pageno = 1
|
|
|
|
scale = 1
|
|
|
|
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
|
2009-11-07 09:12:54 +00:00
|
|
|
elif k == '-n': laparams = None
|
2009-10-24 04:41:59 +00:00
|
|
|
elif k == '-D': laparams.direction = v
|
|
|
|
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-03-22 08:36:39 +00:00
|
|
|
elif k == '-O': outdir = v
|
|
|
|
elif k == '-t': outtype = v
|
|
|
|
elif k == '-c': codec = v
|
|
|
|
elif k == '-s': scale = float(v)
|
2009-10-24 04:41:59 +00:00
|
|
|
#
|
|
|
|
CMapDB.debug = debug
|
|
|
|
PDFResourceManager.debug = debug
|
|
|
|
PDFDocument.debug = debug
|
|
|
|
PDFParser.debug = debug
|
|
|
|
PDFPageInterpreter.debug = debug
|
|
|
|
PDFDevice.debug = debug
|
|
|
|
#
|
2009-12-19 14:17:00 +00:00
|
|
|
rsrc = PDFResourceManager()
|
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':
|
|
|
|
device = TextConverter(rsrc, outfp, codec=codec, laparams=laparams)
|
2009-10-31 03:04:56 +00:00
|
|
|
elif outtype == 'xml':
|
2010-01-31 02:09:28 +00:00
|
|
|
device = XMLConverter(rsrc, outfp, codec=codec, laparams=laparams, outdir=outdir)
|
2009-10-24 04:41:59 +00:00
|
|
|
elif outtype == 'html':
|
2010-01-31 02:09:28 +00:00
|
|
|
device = HTMLConverter(rsrc, outfp, codec=codec, scale=scale, laparams=laparams, outdir=outdir)
|
2009-10-24 04:41:59 +00:00
|
|
|
elif outtype == 'tag':
|
|
|
|
device = TagExtractor(rsrc, outfp, codec=codec)
|
|
|
|
else:
|
|
|
|
return usage()
|
|
|
|
for fname in args:
|
|
|
|
fp = file(fname, 'rb')
|
|
|
|
process_pdf(rsrc, device, fp, pagenos, maxpages=maxpages, password=password)
|
|
|
|
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))
|