-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathxbrl_parser.py
458 lines (344 loc) · 11.8 KB
/
xbrl_parser.py
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
"""
Martin Wood - Office for National Statistics
martin.wood@ons.gov.uk
23/07/2018
XBRL parser
Contains functions that scrape and clean an XBRL document's content
and variables, returning a dict ready for dumping into
MongoDB.
"""
import os
import re
import numpy as np
import pandas as pd
from datetime import datetime
from dateutil import parser
from bs4 import BeautifulSoup as BS # Can parse xml or html docs
# Table of variables and values that indicate consolidated status
consolidation_var_table = {
"includedinconsolidationsubsidiary":True,
"investmententityrequiredtoapplyexceptionfromconsolidationtruefalse":True,
"subsidiaryunconsolidatedtruefalse":False,
"descriptionreasonwhyentityhasnotpreparedconsolidatedfinancialstatements":"exist",
"consolidationpolicy":"exist"
}
def clean_value(string):
"""
Take a value that's stored as a string,
clean it and convert to numeric.
If it's just a dash, it's taken to mean
zero.
"""
if string.strip() == "-":
return(0.0)
try:
return(float(string.strip().replace(",", "").replace(" ", "")))
except:
pass
return(string)
def retrieve_from_context(soup, contextref):
"""
Used where an element of the document contained no data, only a
reference to a context element.
Finds the relevant context element and retrieves the relevant data.
Returns a text string
Keyword arguments:
soup -- BeautifulSoup souped html/xml object
contextref -- the id of the context element to be raided
"""
try:
context = soup.find("xbrli:context", id=contextref)
contents = context.find("xbrldi:explicitmember").get_text().split(":")[-1].strip()
except:
contents = ""
return(contents)
def retrieve_accounting_standard(soup):
"""
Gets the account reporting standard in use in a document by hunting
down the link to the schema reference sheet that always appears to
be in the document, and extracting the format and standard date from
the string of the url itself.
WARNING - That means that there's a lot of implicity hardcoded info
on the way these links are formated and referenced, within this
function. Might need changing someday.
Returns a 3-tuple (standard, date, original url)
Keyword arguments:
soup -- BeautifulSoup souped html/xml object
"""
# Find the relevant link by its unique attribute
link_obj = soup.find("link:schemaref")
# If we didn't find anything it's an xml doc using a different
# element name:
if link_obj == None:
link_obj = soup.find("schemaref")
# extract the name of the .xsd schema file, which contains format
# and date information
text = link_obj['xlink:href'].split("/")[-1].split(".")[0]
# Split the extracted text into format and date, return values
return(text[:-10].strip("-"), text[-10:], link_obj['xlink:href'])
def retrieve_unit(soup, each):
"""
Gets the reporting unit by trying to chase a unitref to
its source, alternatively uses element attribute unitref
if it's not a reference to another element.
Returns the unit
Keyword arguments:
soup -- BeautifulSoup souped html/xml object
each -- element of BeautifulSoup souped object
"""
# If not, try to discover the unit string in the
# soup object
try:
unit_str = soup.find(id=each['unitref']).get_text()
except:
# Or if not, in the attributes of the element
try:
unit_str = each.attrs['unitref']
except:
return("NA")
return(unit_str.strip())
def retrieve_date(soup, each):
"""
Gets the reporting date by trying to chase a contextref
to its source and extract its period, alternatively uses
element attribute contextref if it's not a reference
to another element.
Returns the date
Keyword arguments:
soup -- BeautifulSoup souped html/xml object
each -- element of BeautifulSoup souped object
"""
# Try to find a date tag within the contextref element,
# starting with the most specific tags, and starting with
# those for ixbrl docs as it's the most common file.
date_tag_list = ["xbrli:enddate",
"xbrli:instant",
"xbrli:period",
"enddate",
"instant",
"period"]
for tag in date_tag_list:
try:
date_str = each['contextref']
date_val = parser.parse(soup.find(id=each['contextref']).find(tag).get_text()).\
date().\
isoformat()
return(date_val)
except:
pass
try:
date_str = each.attrs['contextref']
date_val = parser.parse(each.attrs['contextref']).\
date().\
isoformat()
return(date_val)
except:
pass
return("NA")
def parse_element(soup, element):
"""
For a discovered XBRL tagged element, go through, retrieve its name
and value and associated metadata.
Keyword arguments:
soup -- BeautifulSoup object of accounts document
element -- soup object of discovered tagged element
"""
if "contextref" not in element.attrs:
return({})
element_dict = {}
# Basic name and value
try:
# Method for XBRLi docs first
element_dict['name'] = element.attrs['name'].lower().split(":")[-1]
except:
# Method for XBRL docs second
element_dict['name'] = element.name.lower().split(":")[-1]
element_dict['value'] = element.get_text()
element_dict['unit'] = retrieve_unit(soup, element)
element_dict['date'] = retrieve_date(soup, element)
# If there's no value retrieved, try raiding the associated context data
if element_dict['value'] == "":
element_dict['value'] = retrieve_from_context(soup, element.attrs['contextref'])
# If the value has a defined unit (eg a currency) convert to numeric
if element_dict['unit'] != "NA":
element_dict['value'] = clean_value(element_dict['value'])
# Retrieve sign of element if exists
try:
element_dict['sign'] = element.attrs['sign']
# if it's negative, convert the value then and there
if element_dict['sign'].strip() == "-":
element_dict['value'] = 0.0 - element_dict['value']
except:
pass
return(element_dict)
def parse_elements(element_set, soup):
"""
For a set of discovered elements within a document, try to parse
them. Only keep valid results (test is whether field "name"
exists).
Keyword arguments:
element_set -- BeautifulSoup iterable search result object
soup -- BeautifulSoup object of accounts document
"""
elements = []
for each in element_set:
element_dict = parse_element(soup, each)
if 'name' in element_dict:
elements.append(element_dict)
return(elements)
def summarise_by_sum(doc, variable_names):
"""
Takes a document (dict) after extraction, and tries to extract
a summary variable relating to the financial state of the enterprise
by summing all those named that exist. Returns dict.
Keyword arguments:
doc -- an extracted document dict, with "elements" entry as created
by the 'scrape_clean_elements' functions.
variable_names - variables to find and sum if they exist
"""
# Convert elements to pandas df
df = pd.DataFrame(doc['elements'])
# Subset to most recent (latest dated)
df = df[df['date'] == doc['doc_balancesheetdate']]
total_assets = 0.0
unit = "NA"
# Find the total assets by summing components
for each in variable_names:
# Fault-tolerant, will skip whatever isn't numeric
try:
total_assets = total_assets + df[df['name'] == each].iloc[0]['value']
# Retrieve reporting unit if exists
unit = df[df['name'] == each].iloc[0]['unit']
except:
pass
return({"total_assets":total_assets, "unit":unit})
def summarise_by_priority(doc, variable_names):
"""
Takes a document (dict) after extraction, and tries to extract
a summary variable relating to the financial state of the enterprise
by looking for each named, in order. Returns dict.
Keyword arguments:
doc -- an extracted document dict, with "elements" entry as created
by the 'scrape_clean_elements' functions.
variable_names - variables to find and check if they exist.
"""
# Convert elements to pandas df
df = pd.DataFrame(doc['elements'])
# Subset to most recent (latest dated)
df = df[df['date'] == doc['doc_balancesheetdate']]
primary_assets = 0.0
unit = "NA"
# Find the net asset/liability variable by hunting names in order
for each in variable_names:
try:
# Fault tolerant, will skip whatever isn't numeric
primary_assets = df[df['name'] == each].iloc[0]['value']
# Retrieve reporting unit if it exists
unit = df[df['name'] == each].iloc[0]['unit']
break
except:
pass
return({"primary_assets":primary_assets, "unit":unit})
def summarise_set(doc, variable_names):
"""
Takes a document (dict) after extraction, and tries to extract
summary variables relating to the financial state of the enterprise
by returning all those named that exist. Returns dict.
Keyword arguments:
doc -- an extracted document dict, with "elements" entry as created
by the 'scrape_clean_elements' functions.
variable_names - variables to find and return if they exist.
"""
results = {}
# Convert elements to pandas df
df = pd.DataFrame(doc['elements'])
# Subset to most recent (latest dated)
df = df[df['date'] == doc['doc_balancesheetdate']]
# Find all the variables of interest should they exist
for each in variable_names:
try:
results[each] = df[df['name'] == each].iloc[0]['value']
except:
pass
# Send the variables back to be appended
return(results)
def scrape_elements(soup, filepath):
"""
Parses an XBRL (xml) company accounts file
for all labelled content and extracts the
content (and metadata, eg; unitref) of each
element found to a dictionary
params: filepath (str)
output: list of dicts
"""
# Try multiple methods of retrieving data, I think only the first is
# now needed though. The rest will be removed after testing this
# but should not affect execution speed.
try:
element_set = soup.find_all()
elements = parse_elements(element_set, soup)
if len(elements) <= 5:
raise Exception("Elements should be gte 5, was {}".format(len(elements)))
return(elements)
except:
pass
return(0)
def flatten_data(doc):
"""
Takes the data returned by process account, with its tree-like
structure and reorganises it into a long-thin format table structure
suitable for SQL applications.
"""
# Need to drop components later, so need copy in function
doc2 = doc.copy()
doc_df = pd.DataFrame()
# Pandas should create series, then columns, from dicts when called
# like this
for element in doc2['elements']:
doc_df = doc_df.append(element, ignore_index=True)
# Dump the "elements" entry in the doc dict
doc2.pop("elements")
# Create uniform columns for all other properties
for key in doc2:
doc_df[key] = doc2[key]
return(doc_df)
def process_account(filepath):
"""
Scrape all of the relevant information from
an iXBRL (html) file, upload the elements
and some metadata to a mongodb.
Named arguments:
filepath -- complete filepath (string) from drive root
"""
doc = {}
# Some metadata, doc name, upload date/time, archive file it came from
doc['doc_name'] = filepath.split("/")[-1]
doc['doc_type'] = filepath.split(".")[-1].lower()
doc['doc_upload_date'] = str(datetime.now())
doc['arc_name'] = filepath.split("/")[-2]
doc['parsed'] = True
# Complicated ones
sheet_date = filepath.split("/")[-1].split(".")[0].split("_")[-1]
doc['doc_balancesheetdate'] = datetime.strptime(sheet_date, "%Y%m%d").date().isoformat()
doc['doc_companieshouseregisterednumber'] = filepath.split("/")[-1].split(".")[0].split("_")[-2]
print(filepath)
try:
soup = BS(open(filepath, "rb"), "html.parser")
except:
print("Failed to open: " + filepath)
return(1)
# Get metadata about the accounting standard used
try:
doc['doc_standard_type'], doc['doc_standard_date'], doc['doc_standard_link'] = retrieve_accounting_standard(soup)
except:
doc['doc_standard_type'], doc['doc_standard_date'], doc['doc_standard_link'] = (0, 0, 0)
# Fetch all the marked elements of the document
try:
doc['elements'] = scrape_elements(soup, filepath)
except Exception as e:
doc['parsed'] = False
doc['Error'] = e
try:
return(doc)
except Exception as e:
return(e)