Successfully compartmentalised code, getting closer to moving pdf->text as a module function.

pull/5/head
Cathal Garvey 2015-05-30 16:27:58 +01:00
parent 08cb217983
commit ead8e778a6
1 changed files with 29 additions and 28 deletions

View File

@ -131,8 +131,8 @@ def extract_text(files=[], outfile='-',
if output_dir: if output_dir:
imagewriter = ImageWriter(output_dir) imagewriter = ImageWriter(output_dir)
if six.PY2 and sys.stdin.encoding: # if six.PY2 and sys.stdin.encoding:
password = password.decode(sys.stdin.encoding) # password = password.decode(sys.stdin.encoding)
if output_type == "text" and outfile != "-": if output_type == "text" and outfile != "-":
for override, alttype in ( (".htm", "html"), for override, alttype in ( (".htm", "html"),
@ -149,38 +149,39 @@ def extract_text(files=[], outfile='-',
else: else:
outfp = open(outfile, "wb") outfp = open(outfile, "wb")
rsrcmgr = PDFResourceManager(caching=not disable_caching) # rsrcmgr = PDFResourceManager(caching=not disable_caching)
if output_type == 'text': # if output_type == 'text':
device = TextConverter(rsrcmgr, outfp, codec=codec, laparams=laparams, # device = TextConverter(rsrcmgr, outfp, codec=codec, laparams=laparams,
imagewriter=imagewriter) # imagewriter=imagewriter)
if six.PY3 and outfp == sys.stdout: # if six.PY3 and outfp == sys.stdout:
outfp = sys.stdout.buffer # outfp = sys.stdout.buffer
if output_type == 'xml': # if output_type == 'xml':
device = XMLConverter(rsrcmgr, outfp, codec=codec, laparams=laparams, # device = XMLConverter(rsrcmgr, outfp, codec=codec, laparams=laparams,
imagewriter=imagewriter, # imagewriter=imagewriter,
stripcontrol=strip_control) # stripcontrol=strip_control)
elif output_type == 'html': # elif output_type == 'html':
device = HTMLConverter(rsrcmgr, outfp, codec=codec, scale=scale, # device = HTMLConverter(rsrcmgr, outfp, codec=codec, scale=scale,
layoutmode=layoutmode, laparams=laparams, # layoutmode=layoutmode, laparams=laparams,
imagewriter=imagewriter) # imagewriter=imagewriter)
elif output_type == 'tag': # elif output_type == 'tag':
device = TagExtractor(rsrcmgr, outfp, codec=codec) # device = TagExtractor(rsrcmgr, outfp, codec=codec)
for fname in files: for fname in files:
with open(fname, "rb") as fp: with open(fname, "rb") as fp:
interpreter = PDFPageInterpreter(rsrcmgr, device) extract_text_to_fp(fp, **locals())
for page in PDFPage.get_pages(fp, # interpreter = PDFPageInterpreter(rsrcmgr, device)
page_numbers, # for page in PDFPage.get_pages(fp,
maxpages=maxpages, # page_numbers,
password=password, # maxpages=maxpages,
caching=not disable_caching, # password=password,
check_extractable=True): # caching=not disable_caching,
page.rotate = (page.rotate + rotation) % 360 # check_extractable=True):
interpreter.process_page(page) # page.rotate = (page.rotate + rotation) % 360
device.close() # interpreter.process_page(page)
# device.close()
return outfp return outfp
# main # main