Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pdf2csv: errors handling + formatting args on the CLI #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions scraptils/tools/pdf2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
self.encoder = codecs.getincrementalencoder(encoding)()

def writerow(self, row):
self.writer.writerow([s.encode("utf-8") if isinstance(s, basestring) else s
for s in row])
try:
self.writer.writerow(
[s.encode("utf-8") if isinstance(s, basestring) else s
for s in row])
except UnicodeDecodeError:
self.writer.writerow( ["?"*len(s) for s in row] )

# Fetch UTF-8 output from the queue ...
data = self.queue.getvalue()
data = data.decode("utf-8")
Expand All @@ -42,7 +47,7 @@ def writerows(self, rows):
self.writerow(row)


def pdf2csv(pdf):
def pdf2csv(pdf, **kwds):
fp = open(pdf, 'rb')
parser = PDFParser(fp)
doc = PDFDocument()
Expand All @@ -57,7 +62,7 @@ def pdf2csv(pdf):
device = PDFPageAggregator(rsrcmgr, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)

writer = UnicodeWriter(sys.stdout)
writer = UnicodeWriter(sys.stdout, **kwds)
for pageno, page in enumerate(doc.get_pages()):
interpreter.process_page(page)
layout = device.get_result()
Expand Down Expand Up @@ -94,6 +99,9 @@ def pdf2csv(pdf):
fp.close()

def filterclose(lst):
if not lst:
return []

tmp=[lst[0]]
for elem in islice(lst, 1, None):
if elem - 2 > tmp[-1]:
Expand All @@ -117,4 +125,14 @@ def get_region(pdf, page, x1,y1,x2,y2):
)

if __name__=='__main__':
pdf2csv(sys.argv[1])
filename=sys.argv[1]
fmtargs = sys.argv[2:]
if fmtargs:
kwargs = {}
for arg in fmtargs:
key,val = arg.strip().split("=")
kwargs[key] = val
pdf2csv(filename, **kwargs)
else:
pdf2csv(filename)