Using Gepsio with Python - JeffFerguson/gepsio GitHub Wiki

Gepsio is built as a .NET assembly, but, thanks to the pythonnet library, Gepsio can be used from Python code.

Installing .NET

Since Gepsio is built as a .NET assembly, the machine executing the Gepsio Python code will need to have .NET installed.

Installing pythonnet

Install pythonnet as part of your project. Execute the command pip install pythonnet to get pythonnet installed.

Loading the .NET CLR

Next, import load from pythonnet, loading and importing the .NET CLR as follows:

from pythonnet import load
load("coreclr")
import clr

Referencing Gepsio

Next, load and reference Gepsio as follows:

clr.AddReference("JeffFerguson.Gepsio")
from JeffFerguson.Gepsio
import XbrlDocument

The XbrlDocument class is the only Gepsio class that must be explicitly imported. All of the other classes will be available through property references.

Once Gepsio is imported and referenced, you can use all of Gepsio's methods and properties within Python.

Example 1: Printing XBRL Object Counts

This Python example loads an XBRL instance into Gepsio and outputs the number of facts, units, and context found in the loaded document.

# WHAT YOU'LL NEED
# ================
# .NET
# pythonnet (install with "pip install pythonnet")
# Gepsio (download from https://www.nuget.org/packages/Gepsio)

from pythonnet import load
load("coreclr")
import clr

clr.AddReference("JeffFerguson.Gepsio")
from JeffFerguson.Gepsio
import XbrlDocument

doc = XbrlDocument()
doc.Load("http://xbrlsite.com/US-GAAP/BasicExample/2010-09-30/abc-20101231.xml")
print(doc.XbrlFragments.Count)
firstFragment = doc.XbrlFragments[0]
print(firstFragment.Facts.Count)
print(firstFragment.Units.Count)
print(firstFragment.Contexts.Count)

Example 2: Print Calculation Concepts

This more complicated Python example loads an XBRL instance into Gepsio and outputs summation concepts and related information defined by the loaded XBRL document's calculation linkbase.

# WHAT YOU'LL NEED
# ================
# .NET 8
# pythonnet (install with "pip install pythonnet")
# Gepsio (download from https://www.nuget.org/packages/Gepsio)

def PrintFacts(fragment):
    print("Number of facts.........: " + str(fragment.Facts.Count))

def PrintUnits(fragment):
    print("Number of units.........: " + str(fragment.Units.Count))

def PrintContexts(fragment):
    print("Number of contexts......: " + str(fragment.Contexts.Count)) 

def PrintSummationConcept(summationConcept):
    print("+-------------------+")
    print("| Summation Concept |")
    print("+-------------------+")
    print("Name: " + summationConcept.SummationConceptLocator.HrefResourceId)
    for currentContributingConceptLocator in summationConcept.ContributingConceptLocators:
        print("Contributor: " + currentContributingConceptLocator.HrefResourceId)

def PrintCalculationLinkbaseInformationForGivenSchema(currentSchema):
    print("Schema location.........: " + currentSchema.LoadPath)
    for currentCalculationLinkbase in currentSchema.CalculationLinkbases:
        for currentCalculationLink in currentCalculationLinkbase.CalculationLinks:
            for currentSummationConcept in currentCalculationLink.SummationConcepts:
                PrintSummationConcept(currentSummationConcept)

from pythonnet import load
load("coreclr")
import clr

clr.AddReference("JeffFerguson.Gepsio")
from JeffFerguson.Gepsio
import XbrlDocument

doc = XbrlDocument()
doc.Load("https://www.sec.gov/Archives/edgar/data/1018724/000101872425000004/amzn-20241231_htm.xml")

print("XBRL load path..........: " + doc.Filename)
print("Number of XBRL fragments: " + str(doc.XbrlFragments.Count))

firstFragment = doc.XbrlFragments[0]
PrintFacts(firstFragment)
PrintUnits(firstFragment)
PrintContexts(firstFragment)
for currentSchema in firstFragment.Schemas:
    PrintCalculationLinkbaseInformationForGivenSchema(currentSchema)