-
Notifications
You must be signed in to change notification settings - Fork 28
/
pdfxml2csv
executable file
·227 lines (188 loc) · 7.57 KB
/
pdfxml2csv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/python
# *-* coding: utf-8 *-*
import csv
import argparse
import operator
import numpy
import math
import lxml.etree
import lxml.html
from pprint import pprint as pprint_orig
def pprint(obj, *args, **kwargs):
width = kwargs.pop('width', 120)
pprint_orig(obj, *args, width=width, **kwargs)
def parse_xml(filename):
pdfxml = open(filename, 'r').read()
root = lxml.etree.fromstring(pdfxml)
pages = []
for pagenum, page in enumerate(root):
assert page.tag == 'page'
pageinfo = {
'pagenumber': page.attrib.get('number'),
'width': page.attrib.get('width'),
'height': page.attrib.get('height'),
}
textboxes = []
for v in page:
if v.tag == 'text':
left = int(v.attrib.get('left'))
top = int(v.attrib.get('top'))
width = int(v.attrib.get('width'))
height = int(v.attrib.get('height'))
info = {
'left': left,
'top': top,
'width': width,
'height': height,
'text': v.text,
}
textboxes.append(info)
pages.append((pageinfo, textboxes))
return pages
def pages2lines(pages):
rows = []
# first create lines from given XML
for pagenum, page in enumerate(pages):
pagelines = {}
for item in page[1]:
top = item['top']
# fix some off-by-one placement issues, which make some text span over two lines where it should be in one
if (top - 1) in pagelines:
top = top - 1
elif (top + 1) in pagelines:
top = top + 1
line = pagelines.setdefault(top, [])
line.append(item)
ordered = list(sorted([(k, sorted(v, key=operator.itemgetter('left'))) for k, v in pagelines.iteritems()]))
rows.extend(ordered)
return rows
def lines2cols(rows, splits, header=-1, footer=-1, verbose=False):
records = []
# splits must containt catchall zero
if 0 not in [i[0] for i in splits]:
splits = [(0, 'left')] + splits
splits = list(sorted(splits))
for topoffset, line in rows:
if footer != -1 and topoffset > footer:
continue
if header != -1 and topoffset < header:
continue
linerec = [[] for i in xrange(len(splits))]
for item in sorted(line, reverse=True):
left = item['left']
idx = len(splits) - 1
while splits[idx][0] > left:
idx = idx - 1
linerec[idx].insert(0, (left, item))
if verbose:
pprint(linerec)
ordered = [sorted(i) for i in linerec]
records.append(ordered)
return records
def records2csv(table, csv_filename, csv_sep, meta):
csvfd = open(csv_filename, 'wb')
csvw = csv.writer(csvfd)
for row in table:
row2 = []
for idx, key in meta:
try:
fieldinfo = row[idx][0][1]
row2.append(str(fieldinfo[key]))
except IndexError:
row2.append('')
for r in row:
row2.append(csv_sep.join([i[1]['text'] for i in r]))
csvw.writerow([i.encode('utf-8') for i in row2])
csvfd.close()
def find_gaps(array, threshold=0):
"""finds "gaps", that is runs of values of less or equal to threshold in 1D numpy array"""
gap_start = 0
prev = None
n = 0
gaps = []
while n < len(array):
cur = array[n]
if prev is None:
prev = cur
continue
if prev > threshold and cur <= threshold:
gap_start = n
if prev <= threshold and cur > threshold:
# end gap
gaps.append((gap_start, n - gap_start))
prev = cur
n += 1
if cur <= threshold and prev <= threshold:
gaps.append((gap_start, n - gap_start))
return gaps
def estimate_cell_vertical(pages, wlen=6):
pagegaps = []
for pageinfo, page in pages:
# pagepoints = [0]*(int(float(pageinfo['height']))+1)
pagepoints = numpy.zeros(int(float(pageinfo['height'])) + 1)
for item in page:
for idx in xrange(item['top'], item['top'] + item['height']):
pagepoints[idx] += len(item['text'])
window = numpy.hamming(wlen)
smoothed = numpy.convolve(pagepoints, window)
gaps = find_gaps(smoothed)
pagegaps.append(gaps)
return pagegaps
def estimate_columns(pages, wlen=6):
maxwidth = max([int(math.ceil(float(pageinfo['width']))) for pageinfo, page in pages])
counts = numpy.zeros(maxwidth, numpy.uint32)
for pageinfo, page in pages:
for item in page:
if item['text'].strip():
counts[item['left']:item['left'] + item['width']] += 1
window = numpy.hamming(wlen)
smoothed = numpy.convolve(counts, window) / 3
avg = sum(counts) / len(counts)
gaps = find_gaps(smoothed, threshold=avg * 0.1)
pos = [(i[0] + i[0] + i[1]) / 2 for i in gaps if i[0] != 0]
print 'Recommended --left parameter:'
print '--left %s' % ','.join([str(i) for i in pos])
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Parse PDF XML into a CSV')
parser.add_argument('filename', metavar='filename.xml', type=str, nargs=1, help='input XML')
parser.add_argument('--verbose', dest='verbose', action='store_true', default=False, help='verbose output')
parser.add_argument('--header', dest='header', type=int, nargs='?', default=-1, help='exclude all content on each page above this')
parser.add_argument('--footer', dest='footer', type=int, nargs='?', default=-1, help='exclude all content on each page below this')
parser.add_argument('--left', dest='left', type=str, nargs='?', help='position of a new cell split')
parser.add_argument('--meta', dest='meta', type=str, nargs='?', help='include metadata about fields')
parser.add_argument('--estimate-vcell', dest='estimate_vcell', action='store_true', help='perform a vertical cell estimation')
parser.add_argument('--estimate-columns', dest='estimate_columns', action='store_true', help='perform a column separator estimation')
parser.add_argument('--csv', dest='output', type=str, nargs='?', help='output CSV file name')
parser.add_argument('--csv-textbox-separator', dest='csv_sep', type=str, default=u' ', nargs='?', help='output CSV separator for joining PDF TextBox elements')
args = parser.parse_args()
verbose = args.verbose
if args.left:
leftsplits = list(sorted([(int(i.strip()), 'left') for i in args.left.split(',')]))
else:
leftsplits = [(0, 'left')]
pages = parse_xml(args.filename[0])
rows = pages2lines(pages)
table = lines2cols(rows,
header=args.header,
footer=args.footer,
splits=leftsplits,
verbose=verbose)
if args.estimate_columns:
gaps = estimate_columns(pages)
if args.estimate_vcell:
gaps = estimate_cell_vertical(pages)
meta = []
if args.meta:
for item in args.meta.split(','):
if '-' not in item:
raise ValueError("invalid meta var name")
field, key = item.split('-')
if key not in ('left', 'width', 'top', 'height'):
raise ValueError("invalid meta var name")
try:
field = int(field)
except ValueError:
raise ValueError("invalid meta field ref index")
meta.append((field, key))
if args.output:
records2csv(table, args.output, args.csv_sep, meta)